一、实验环境
1.Windows10x64
2.anaconda4.6.9 + python3.7.1(anaconda集成,不需单独安装)
二、代码示例
# -*- coding: utf-8 -*-
import os
from configparser import ConfigParser, NoSectionError
debug = False
def debug_print(str):
if debug:
print(str)
class GetConfig(ConfigParser):
def __init__(self, defaults=None):
"""
初始化函数
:param defaults:
"""
ConfigParser.__init__(self, defaults=None)
def optionxform(self, optionstr):
"""
解决ConfigParser将键值对强制转换为小写问题
:param optionstr:
:return:
"""
return optionstr
def open_file(self, file_name):
"""
打开config文件夹
:param file_name: config文件路径
:return:
"""
if os.path.exists(file_n
这篇博客介绍了如何在Python中使用ConfigParser模块来读取和操作配置文件。通过GetConfig类,演示了打开配置文件、获取所有section、检查section、获取section中的选项和键值对、以及通过字符串匹配选项等功能。
最低0.47元/天 解锁文章
1061

被折叠的 条评论
为什么被折叠?



