【问题标题】:Python ConfigParser with colon in the key键中带有冒号的 Python ConfigParser
【发布时间】:2013-07-30 12:41:21
【问题描述】:

How do I put a semicolon in a value in python configparser?

Python - 2.7

我有一个 python 配置解析器,其中键是 url,值是令牌。作为 url 的键包含 :, -, ?和其他各种字符同样适用于价值。从上面的问题可以看出,值部分中的特殊字符似乎没问题,但键似乎没问题。

对此我能做些什么吗?我的替代方案是解析为 json 文件并手动手动写入/读取它。

例如,如果你在我得到后运行下面的程序

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")

文件如下所示

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

我得到了例外ConfigParser.NoOptionError: No option 'http://myhost.com:9090' in section: 'section'

【问题讨论】:

    标签: python python-2.7


    【解决方案1】:

    Python 2.7 上的ConfigParser 被硬编码以将冒号和等号识别为键和值之间的分隔符。当前的 Python 3 configparser 模块允许您自定义分隔符。 Python 2.6-2.7 的反向移植可在 https://pypi.python.org/pypi/configparser

    获得

    【讨论】:

      【解决方案2】:
      1. 拆分您的 URL 协议、基础和端口,即 : 之后的位,并将它们用作辅助键 OR
      2. 替换:使用允许的东西,反之亦然,可能使用 0xnn 符号或类似的东西 OR
      3. 您可以使用基于 URL 的值,例如 URL 值的 MD5 作为键。

      【讨论】:

        【解决方案3】:

        我通过将ConfigParser 使用的正则表达式更改为仅使用= 作为分隔符解决了类似的问题。

        这已经在 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>.*)$'                   
                )
        

        【讨论】:

          【解决方案4】:

          http://bugs.python.org/issue16374

          分号是 2.7 中的内联注释分隔符

          【讨论】:

            【解决方案5】:

            您可以使用以下解决方案来执行您的任务

            ConfigParser中允许的“_”或“-”等特定特殊字符替换所有冒号

            代码:

            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("_",":")
            

            输出:

            用户:id:令牌

            【讨论】:

              猜你喜欢
              • 2011-05-02
              • 1970-01-01
              • 1970-01-01
              • 2015-07-08
              • 2012-02-26
              • 1970-01-01
              • 2012-03-26
              • 1970-01-01
              • 2011-09-11
              相关资源
              最近更新 更多