python - 配置解析器 : Choosing name and value delimiter

标签 python configparser

假设我有一个包含以下行的 test.ini 文件:

[A]
name1 [0,1]=0
name2 a:b:c / A:B:C [0,1]=1

当我这样解析它时:

A = ConfigParser.ConfigParser()
with codecs.open('test.ini', 'r') as f:
    A.optionxform = str
    A.readfp(f)

for section_name in A.sections():
    print 'Section:', section_name
    print 'Options:', A.options(section_name)
    for name, value in A.items(section_name):
        print 'name-value pair:'
        print '%s' % (name)
        print '%s' % (value)

我得到以下输出:

Section: A
Options: ['name1 [0,1]', 'name2 a']
name-value pair:
name1 [0,1]
0
name-value pair:
name2 a
b:c / A:B:C [0,1]=1

但这不是我想要的, 我希望它是这样的:

Section: A
Options: ['name1 [0,1]', 'name2 a:b:c / A:B:C [0,1]']
name-value pair:
name1 [0,1]
0
name-value pair:
name2 a:b:c / A:B:C [0,1]
1

有没有办法以某种方式选择定界符 name 和 value 之间只能是 = 符号?

如果一行中不止一个=, 分隔符是最后一个吗?

最佳答案

问题通过跳到 Python 3.3 和:A = configparser.ConfigParser(delimiters=('='))

关于python - 配置解析器 : Choosing name and value delimiter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21328509/

相关文章:

python - ConfigParser 从空值中删除引号

python configparser写入默认部分

python - 设置 python emacs 环境 mac os x

Python 配置分析器 : get list of unused entries

python - 如何使用python动态地将不一致的字典转换为.ini?

python - ConfigObj 或 ConfigParser 哪个更好?

python - 属性和 __getattr__ 与 AttributeError 的兼容性问题

python - 导入父模块和子模块

Python - 用空字典/数组替换空值

python - 过帐付款数据