python-3.x - Python configparser 自动转换为布尔值
问题描述
我正在编写一个类来解析各种配置文件。它包含yaml
文件和ini
+cfg
文件的方法。
解析 yamls 工作正常,但我对另外两个有问题。基本上我阅读它们,我解析它们,但是如果我的config.cfg
文件看起来像这样:
[foo]
bar: True
的值为config['foo']['bar']
will 'True'
, not True
,所以我最终得到一个字符串。
我不想强迫我班级的用户转换给定的值。有没有办法解决这个问题,所以字符串变成布尔值?
PS阅读配置后,我转换返回读取值的字典,如下所示:
config_parser = configparser.ConfigParser()
config_parser.read(config_file_path)
return {section: dict(config_parser.items(section)) for section in config_parser.sections()}
解决方案
推荐阅读
- php - check id in all tables have the prefix table_
- java - 有没有办法通过在杰克逊中包含数组来确定抽象对象的目标类型?
- android - Nativescript:获取 RadListView 项目的视图
- ajax - 如何通过 AJAX 处理 Stripe 支付失败,并通过标准页面加载成功?(使用 Laravel 后端)
- amazon-web-services - 如何设置 API 网关与私有 NLB 通信?
- mysql - PHP select from 3 tables
- python - Show tuple in a table Kivy
- azure - Azure 资源提供程序注册
- vb.net - Give a custom name to a process started in VB.Net
- android - 如何解决内存泄漏问题?