简体   繁体   English

关键字中带有冒号的Python ConfigParser

[英]Python ConfigParser with colon in the key

How do I put a semicolon in a value in python configparser? 如何在Python configparser中的值中添加分号?

Python - 2.7 Python-2.7

I have a python config parser with a section where the Key is a url and the value is a token. 我有一个带有部分的python配置解析器,其中Key是url,值是令牌。 The key being a url contains :, -, ? 网址关键字为:,-,?。 and various other chars same applies to value. 其他各种字符也适用于价值。 As you can see from the above question the special chars in the value section seems to be fine but the key does not appear to be fine. 从上面的问题中可以看出,值部分中的特殊字符似乎很好,但是键似乎没有问题。

Is there anything I can do about this? 有什么我可以做的吗? My alternatives are resolving to a json file and manually writing/reading it manually. 我的替代方法是解析为json文件并手动写入/手动读取。

For example if you run the below program once I get 例如,如果您在运行以下程序后运行以下程序

cp = ConfigParser.ConfigParser()
cp.add_section("section")
cp.set("section", "http://myhost.com:9090", "user:id:token")
cp.set("section", "key2", "value2")
with open(os.path.expanduser("~/test.ini"), "w") as f:
    cp.write(f)

cp = ConfigParser.ConfigParser()
cp.read(os.path.expanduser("~/test.ini"))
print cp.get("section", "key2")
print cp.get("section", "http://myhost.com:9090")

the file looks like below 该文件如下所示

[section]
http://myhost.com:9090 = user:id:token
key2 = value2

And I get exceptions ConfigParser.NoOptionError: No option 'http://myhost.com:9090' in section: 'section' 而且我得到异常ConfigParser.NoOptionError: No option 'http://myhost.com:9090' in section: 'section'

ConfigParser on Python 2.7 is hard-coded to recognize both the colon and the equals sign as delimiters between keys and values. Python 2.7上的ConfigParser进行了硬编码,以将冒号和等号识别为键和值之间的分隔符。 The current Python 3 configparser module allows you to customize the delimiters. 当前的Python 3 configparser模块允许您自定义分隔符。 A backport for Python 2.6-2.7 is available at https://pypi.python.org/pypi/configparser https://pypi.python.org/pypi/configparser提供了Python 2.6-2.7的反向端口

  1. Split out your URL protocol, base and the port, ie the bits after the : and use them as a secondary keys OR 拆分URL协议,基本和端口,即:之后的位,并将它们用作辅助键,
  2. Replace : with something allowed and vice-versa, possibly using a 0xnn notation or something similar OR 替换:允许的内容,反之亦然,可能使用0xnn表示法或类似的内容,
  3. You could use a value based on the URL such as a MD5 of the URL value as your key. 您可以使用基于URL的值(例如URL值的MD5)作为密钥。

I solved a similar problem by changing the regular expression used by ConfigParser to only use = as separator. 我通过将ConfigParser使用的正则表达式ConfigParser为仅将=用作分隔符来解决了类似的问题。

This has been tested on Python 2.7.5 and 3.4.3 已在Python 2.7.5和3.4.3上进行了测试

import re
try:
    # Python3
    import configparser
except:
    import ConfigParser as configparser

class MyConfigParser(configparser.ConfigParser):
    """Modified ConfigParser that allow ':' in keys and only '=' as separator.
    """
    OPTCRE = re.compile(
        r'(?P<option>[^=\s][^=]*)'          # allow only = 
        r'\s*(?P<vi>[=])\s*'                # for option separator           
        r'(?P<value>.*)$'                   
        )

see http://bugs.python.org/issue16374 参见http://bugs.python.org/issue16374

semicolons are inline comment delimiters in 2.7 分号是2.7中的内联注释定界符

You can use following solution to perform your task 您可以使用以下解决方案执行任务

Replace all colons with specific special characters such as "_" or "-", which are allowed in ConfigParser 将所有冒号替换为ConfigParser中允许的特定特殊字符,例如“ _”或“-”

Code: 码:

from ConfigParser import SafeConfigParser

cp = SafeConfigParser()
cp.add_section("Install")
cp.set("Install", "http_//myhost.com_9090", "user_id_token")
with open("./config.ini", "w") as f:
    cp.write(f)

cp = SafeConfigParser()
cp.read("./config.ini")
a = cp.get("Install", "http_//myhost.com_9090")
print a.replace("_",":")

Output: 输出:

user:id:token 用户:ID:令牌

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM