测试开发技术网站
博客
设计
设计
开发
Python
测试
unittest
运维
Linux基础应用
CI/CD
CI/CD
数据库
数据库
云计算
云计算
云原生
云原生
爬虫
爬虫
数据分析
数据分析
人工智能
人工智能
登录
注册
Python如何处理配置文件即configparser模块的用法
收藏本文
作者:redrose2100 类别: 日期:2022-05-11 02:01:26 阅读:945 次 消耗积分:0 分
### 1 configparser 使用get方式读取.ini配置文件的配置内容 * (1)首先编写如下所示的env.ini配置文件 ```·python [server] ip=192.168.1.200 port=22 username=root password=root [personal] name=redrose2100 city=nanjing github=redrose2100.github.io ``` * (2) 编写解析.ini配置文件的python代码 ```python import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config.get("server","ip")) print(config.get("personal","name")) ``` 运行结果为: ```python 192.168.1.200 redrose2100 ``` ### 2 使用数组下标的方式读取.ini配置文件的内容 env.ini的内容同上述1中的内容 通过数组下标的方式读取配置文件内容的代码如下: ```python import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config["server"]["ip"]) print(config["personal"]["name"]) ``` 执行结果如下: ```python 192.168.1.200 redrose2100 ``` ### 3 使用configparser写配置文件 ```python import configparser config=configparser.ConfigParser() config["server"]={ "ip":"192.138.1.200", "port":22, "username":"root", "password":"root" } config["personal"]={ "name":"redrose2100", "city":"nanjing" } with open("test.ini","w") as f: config.write(f) ``` 执行之后,在当前目录下会生成一个test.ini文件,其内容如下: ```python [server] ip = 192.138.1.200 port = 22 username = root password = root [personal] name = redrose2100 city = nanjing ``` ### 4 configparser 对section常用的操作: * (1)has_section(section) 判断读取的config对象是否还有指定的section * (2)sections() 获取读取到的config对象的所有sections列表 * (3)add_section(section) 给读取到的config对象增加一个section,注意此时增加的section只是在config对象中,并没有写入到ini配置文件中 * (4)remove_section(section) 给读取到的config对象删除一个section 实例代码如下所示: ```python import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config.has_section("server")) print(config.sections()) config.add_section("kafka") print(config.sections()) config.remove_section("kafka") print(config.sections()) ``` 运行结果如下: ```python import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config.has_section("server")) print(config.sections()) config.add_section("kafka") print(config.sections()) config.remove_section("kafka") print(config.sections()) ``` ### 5 configparser对option常用的操作,如下代码演示: ```python import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config.has_option("server","ip")) print(config.options("server")) config.set("server","test","test") print(config.options("server")) config.remove_option("server","test") print(config.options("server")) ``` 执行结果为: ```python True ['ip', 'port', 'username', 'password'] ['ip', 'port', 'username', 'password', 'test'] ['ip', 'port', 'username', 'password'] ``` ### 6 configparser的对象可以类似字典一样使用,但是类型不是字典,代码演示如下: ```python import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") for key in config["server"].keys(): print(key) for key,value in config["server"].items(): print(key,value) for value in config["server"].values(): print(value) item=config["server"].popitem() print(type(item)) print(item) port=config["server"].pop("port") print(port) for key,value in config["server"].items(): print(key,value) username=config["server"].get("username","no found") print(username) print(type(config["server"])) ``` 运行结果如下: ```python ip port username password ip 192.168.1.200 port 22 username root password root 192.168.1.200 22 root root
('ip', '192.168.1.200') 22 username root password root root
``` ### 7 可以将获取的类型直接转换为期望的数据类型,可用的方法有: * getint * getboolean * getfloat * get 下面将配置文件更新如下内容: ```pyhton [server] ip=192.168.1.200 port=22 username=root password=root is_linux=True price=100.24 [personal] name=redrose2100 city=nanjing github=redrose2100.github.io ``` 实例代码如下: ```python import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") ip=config["server"].get("ip") port=config["server"].getint("port") is_linux=config["server"].getboolean("is_linux") price=config["server"].getfloat("price") print(ip,type(ip)) print(port,type(port)) print(is_linux,type(is_linux)) print(price,type(price)) ``` 运行结果如下: ```python 192.168.1.200
22
True
100.24
``` ### 8 configparser标准库对解析.conf文件与解析.ini文件的使用方法是完全一样的,下面只演示一部分: 创建一个env.conf文件,内容如下: ```python [server] ip=192.168.1.200 port=22 username=root password=root is_linux=True price=100.24 [personal] name=redrose2100 city=nanjing github=redrose2100.github.io ``` 编写如下代码: ```python import configparser config=configparser.ConfigParser() config.read("env.conf","utf-8") print(config.get("server","ip")) print(config.get("personal","name")) print(config["server"]["ip"]) print(config["personal"]["name"]) ``` 运行结果如下: ```python 192.168.1.200 redrose2100 192.168.1.200 redrose2100 ```
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/148
上一篇:
Python----循序渐进基础入门系列
下一篇:
Python如何打包即setuptools模块的用法
搜索
个人成就
出版书籍
《Pytest企业级应用实战》
测试开发技术全栈公众号
测试开发技术全栈公众号
DevOps技术交流微信群
加微信邀请进群
常用网站链接
开源软件洞察
云原生技术栈全景图
Python语言官方文档
Golang官方文档
Docker官方文档
Jenkins中文用户手册
Scrapy官方文档
VUE官方文档
Harbor官方文档
openQA官方文档
云原生开源社区
开源中国
Kubernetes中文文档
Markdown语法官方教程
Kubernetes中文社区
Kubersphere官方文档
BootStrap中文网站
JavaScript中文网
NumPy官方文档
Pandas官方文档
GitLink确实开源网站
数据库排名网站
编程语言排名网站
SEO综合查询网站
数学加减法练习自动生成网站
Kickstart Generator
文章分类
最新文章
最多阅读
特别推荐
×
Close
登录
注册
找回密码
登录邮箱:
登录密码:
图片验证码:
注册邮箱:
注册密码:
邮箱验证码:
发送邮件
注册邮箱:
新的密码:
邮箱验证码:
发送邮件