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 '***********************************'

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM