4、Python 处理 ini 文件
来源:互联网 发布:如何看待网络暴力 知乎 编辑:程序博客网 时间:2024/06/05 11:53
.ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式。
一、.ini 文件示例
下面是一个 .ini 文件:
[School]ip = 10.15.40.123mask = 255.255.255.0gateway = 10.15.40.1dns = 211.82.96.1[Match]ip = 172.17.29.120mask = 255.255.255.0gateway = 172.17.29.1dns = 0.0.0.0
这个配置文件中保存的是不同场合下的IP设置参数。下面将以生成和读取这个配置文件为例,进行讲解。
二、使用Python 读取 ini 文件
import configparserinifile = "a.ini"conf = configparser.ConfigParser() #生成conf对象conf.read(inifile)home_ip = conf.get("School","ip")match_ip = conf.get("Match","ip")print("home_ip = {}".format(home_ip))print("match_ip = {}".format(match_ip))print(conf.sections()) #显示所有节名称print(conf.options('School')) #显示节下面的option名称
三、写 ini 文件
import configparserinifile = "a.ini"conf = configparser.ConfigParser() #生成conf对象conf.add_section("home") #添加一个节conf.set("home","ip","192.168.1.1") #在某个节下面添加key 和 valueconf.write(open(inifile,'w')) #写到某个文件里面
注意open使用w方法会覆盖掉之前的内容,使用a(append)会追加。
三、改 ini 文件内容
import configparserinifile = "a.ini"conf = configparser.ConfigParser() #生成conf对象conf.read(inifile)conf.set("home","ip","1.1.1.2")conf.write(open(inifile,"w"))
四、删 ini 文件
import configparserinifile = "a.ini"conf = configparser.ConfigParser() #生成conf对象conf.read(inifile)conf.remove_section("home")conf.write(open(inifile,'w'))
上面的是删除节
也可以删除option
conf.remove_option("home",'ip')
五、selenium 读取ini 配置文件
redmine.ini 文件内容:
[URL]url = http://localhost:81/redmine/login[mail]host = smtp.163.com
用例读取 ini 配置文件
import configparserfrom selenium import webdriverfrom time import sleepdriver = webdriver.Chrome()inifile = "redmine.ini"conf = configparser.ConfigParser()conf.read(inifile)url = conf.get("URL","url")driver.get(url)sleep(2)driver.quit()
六、改进,读ini文件写成一个函数
引入一个readini.py文件,将读ini文件写成一个函数
import configparserdef readini(filename,section,option): conf = configparser.ConfigParser() conf.read(filename) return conf.get(section, option)用例代码变更为
import configparserfrom selenium import webdriverfrom time import sleepfrom second.readini import readinidriver = webdriver.Chrome()inifile = "redmine.ini"url = readini(inifile,"URL","url")driver.get(url)sleep(2)driver.quit()
七、改进,将filename加一个默认值参数,如果不传的话,默认使用redmine.ini文件
import configparserdef readini(section,option,filename="redmine.ini"): conf = configparser.ConfigParser() conf.read(filename) return conf.get(section, option)用例变为:
import configparserfrom selenium import webdriverfrom time import sleepfrom second.readini import readinidriver = webdriver.Chrome()url = readini("URL","url")driver.get(url)sleep(2)driver.quit()
八、改进
读ini 函数写成方法
import configparserclass ReadINI(): def readini(self,section, option, filename="redmine.ini"): conf = configparser.ConfigParser() conf.read(filename) return conf.get(section, option)用例变为
import configparserfrom selenium import webdriverfrom time import sleepfrom second.readini import ReadINIdriver = webdriver.Chrome()url = ReadINI().readini("URL","url")driver.get(url)sleep(2)driver.quit()
阅读全文
0 0
- 4、Python 处理 ini 文件
- Python 处理config.ini文件
- Python 配置处理类(ini文件)
- Python 处理 ini 格式文件
- python ini配置文件处理
- [Python] 文件读写——处理ini配置文件
- 处理INI文件
- c# INI文件处理
- INI 文件的处理
- python Ini文件读写
- python讀寫ini文件
- python 解析ini 文件
- Python读写ini文件
- Python ini文件操作
- Python 操作ini文件
- Python读写ini文件
- python解析ini文件
- Python解析INI文件
- 推荐一个软件
- openshift/origin工作记录(1)——S2I镜像定制(基于SVN)
- TensorFlow的cnn做mnist例子
- QImage与QPixmap完全解析
- ckplayer播放器控制播放/暂停
- 4、Python 处理 ini 文件
- match返回参数和分组
- 五分钟学GIS | 大数据之分布式空间分析服务
- java调用WebService接口的几种方法
- C++中的基础相关知识之预处理,const,sizeof
- 桌面应用程序与web应用程序
- MyEclipse WebSphere开发教程:安装和更新WebSphere 6.1, JAX-WS, EJB 3.0(四)
- 矩阵权(Matrix weighted)Bezier三角(曲面)片
- 关于viewpager与fragment需要刷新数据的bug,解决办法。