python 读取ini 配置文件

安装

pip install configparser

1 配置文件 config.ini:

[MysqlDB]user=rootpasswd=123456sport=3306db_name=my_dbcharset=utf-8

获取参数:

import configparser

config = configparser.ConfigParser()     

config.read(‘config.ini‘)

host=config[‘MysqlDB‘][‘host‘]

host=config.get(‘MysqlDB‘,‘host‘) (str格式)

sport=cfg.getint(‘MysqlDB‘,‘sport‘) (int格式)

  

原文地址:https://www.cnblogs.com/cyanrose/p/12073731.html

时间: 2024-08-05 05:54:53

python 读取ini 配置文件的相关文章

Python读取ini配置文件

db_config.ini [baseconf] host=127.0.0.1 port=3306 user=root password=root db_name=evaluting_sys [concurrent] processor=20 python代码 1 对应的python代码 2 import sys,os 3 import ConfigParser 4 5 class Db_Connector: 6 def __init__(self, config_file_path): 7 c

python读取ini文件

import configparser import os config=configparser.ConfigParser()#创建config对象 file_path=os.path.dirname(os.path.abspath('.'))+'\Python源码\config.ini'#读取文件父目录 config.read(file_path) sender=config.get('sender','sender')#读取ini配置文件中sender项中的sender值 print(fi

转 python3 读取 ini配置文件

在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: 1 [path] 2 back_dir = /Users/abc/PycharmProjects/Pythoncoding/projects/ 3 target_dir = /Users/abc/PycharmProjects/Pythoncoding/ 4 5 [file] 6 back_file = apitest import osimport timeimport

python3读取ini配置文件

python3读取ini配置文件(含中文)import configparser# 加载现有配置文件conn = configparser.ConfigParser()conn.read("KKD.ini", encoding="utf-8-sig") #此处是utf-8-sig,而不是utf-8 #以下两种方法读取文件内容效果一样print(conn.get('rclog', 'kkdqg_in')) 原文地址:https://www.cnblogs.com/te

python读取yaml配置文件

yaml简介 1.yaml [?j?m?l]: Yet Another Markup Language :另一种标记语言.yaml 是专门用来写配置文件的语言,非常简洁和强大,之前用ini也能写配置文件,看了yaml后,发现这个更直观,更方便,有点类似于json格式 2.yaml基本语法规则: 大小写敏感 使用缩进表示层级关系 缩进时不允许使用Tab键,只允许使用空格. 缩进的空格数目不重要,只要相同层级的元素左侧对齐即可 #表示注释,从这个字符一直到行尾,都会被解析器忽略,这个和python的

C# 读取Ini配置文件类

配置文件 为fileName.ini 的文件 第一行必须为空,不然读不出值 [section1] key=value key2=value ......... [section2] key=value key2=value ......... 代码如下: using System; using System.Runtime.InteropServices; using System.Text; namespace Test { /// <summary> /// INI文件的操作类 /// &

读取ini配置文件

配置文件中经常用到ini文件,在VC中其函数分别为: 写入.ini文件: BOOL WritePrivateProfileString(  LPCTSTR lpAppName,  // INI文件中的一个字段名[节名]可以有很多个节名   LPCTSTR lpKeyName,  // lpAppName 下的一个键名,也就是里面具体的变量名   LPCTSTR lpString,   // 键值,也就是数据   LPCTSTR lpFileName  // INI文件的路径); 读取.ini文件

python读取ini配置的类封装

此为基础封装,未考虑过多异常处理 类 # coding:utf-8 import configparser import os class IniCfg(): def __init__(self): self.conf = configparser.ConfigParser() self.cfgpath = '' def checkSection(self, section): try: self.conf.items(section) except Exception: print(">

python对ini配置文件处理

实例文件: [[email protected] ~]# cat test.ini [base] host = 192.168.88.121 port = 3306 user = root path = /home passwd = 123 [callback] path = /Autops alert = yes count = 1 ftp = no 基础用法:代码片段 >>> cf.read("test.ini")