python 读取配置文件ini ---ConfigParser

Python读取ini文件需要用到 ConfigParser 模块

关于ConfigParser模块的介绍详情请参照官网解释:https://docs.python.org/2.7/library/configparser.html

  • 要读取的ini文件(configfile.ini)如下:
  1. 以"[browserType ]"做section,browserName 为options
[browserType]
#browserName = Firefox
browserName = Chrome
#browserName = IE


[account]
user = test1
#user = admin

[language]
language = EN
#language = CN

[country]
country = US
#country = FR
#country = IT
  •  get(section,option) 得到section中option的值,返回为string类型
  • 在自动化测试框架中读取每个配置文件中的值,并返回以做调用或者拼接

import ConfigParser

class
ReadConfigFile(object): def get_value(self): config = ConfigParser.ConfigParser() config.read("configfile.ini") browserName = config.get('browserType','browserName') account = config.get('accountType','user') language = config.get('languageType','language') country = config.get('countryType','country') return [browserName,account,language,country] trcf = ReadConfigFile() print '***********************************' print trcf.get_value() print '***********************************'

原文地址:https://www.cnblogs.com/lanbing/p/9443812.html