Python中操作配置文件
2021/8/31 11:06:05
本文主要是介绍Python中操作配置文件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
xx.ini [test] host = 127.0.0.1 port = 8080 username = root password = abc status = True data = 123.123 date = 0830 handle_config.py
from configparser import ConfigParser # 1.创建一个配置文件解析器对象 cp = ConfigParser() # 2.读取配置文件的内容到配置解析器中 cp.read("xx.ini", encoding='utf-8') # 3.读取配置内容 # 3.1 get:读取的内容当成字符串 content0 = cp.get("test", "username") print(content0, type(content0)) # 输出 root <class 'str'> content1 = cp.get("test", "port") print(content1, type(content1)) # 输出 8080 <class 'str'> # 3.2 getint:读取数值类型的数据 content2 = cp.getint("test", "port") print(content2, type(content2)) # 输出 8080 <class 'int'> # content3 = cp.getint("test", "username") # 报错 # 3.3 getboolean:读取布尔类型的数据 content4 = cp.getboolean("test", "status") print(content4, type(content4)) # 输出 True <class 'bool'> # 3.4 getfloat:读取浮点类型的数据 content5 = cp.getfloat("test", "data") print(content5, type(content5)) # 输出 123.123 <class 'float'>
这篇关于Python中操作配置文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-18初探Python股票自动化交易:入门指南
- 2024-09-18Python量化入门:轻松掌握量化分析基础与实战
- 2024-09-18Python量化交易:入门指南与实践
- 2024-09-18Python量化交易:入门指南与实战技巧
- 2024-09-14Python人工智能项目实战:从零开始的实践指南
- 2024-09-14探索Python人工智能资料:初学者的指南
- 2024-09-14Python人工智能资料:初学者的全面指南
- 2024-09-13Matplotlib入门:轻松绘制Python数据可视化图表
- 2024-09-13Python人工智能:初学者的入门指南
- 2024-09-13Python人工智能:轻松入门与实践