python讀取ini配置文件

python讀取配置文件的方法:python

1. 引入庫python3.x

python2.x函數

import ConfigParser

python3.xspa

import configparser

區別:python2.x每一個單詞開頭都是大寫,python3.x都是小寫code

2. 打開文件blog

python2.xget

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

python3.xinput

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

3. 讀取信息io

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

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

讀取信息經過get系列函數:class

參數1是節(section);

參數2是鍵;

返回的是值。

相關文章
相關標籤/搜索