python读取ini配置文件

python读取配置文件的方法:

1. 引入库

python2.x

import ConfigParser

python3.x

import configparser

区别:python2.x每个单词开头都是大写,python3.x都是小写

2. 打开文件

python2.x

cf = ConfigParser.ConfigParser()
cf.read('./config.ini')

python3.x

cf = configparser.ConfigParser()
cf.read('./config.ini', encoding='gbk')

3. 读取信息

input_path = cf.get('input', 'path')

trade_type = cf.getint('trade', 'trade_type')

读取信息通过get系列函数:

参数1是节(section);

参数2是键;

返回的是值。

原文地址:https://www.cnblogs.com/zhugaopeng/p/9745746.html