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是键;

返回的是值。

相关文章:

  • 2022-12-23
  • 2021-09-05
  • 2021-10-12
  • 2021-10-14
  • 2022-12-23
  • 2021-10-21
  • 2021-10-27
  • 2022-12-23
猜你喜欢
  • 2021-12-26
  • 2022-01-20
  • 2022-12-23
  • 2022-12-23
  • 2021-12-16
  • 2021-12-20
  • 2021-11-26
相关资源
相似解决方案