【问题标题】:how can Parser a file(ini) include "AT" by python解析文件(ini)如何通过python包含“AT”
【发布时间】:2015-10-16 03:51:41
【问题描述】:
[db_finding]
cluster_name  :  db_finding
connect_timeout_ms  :  900

[.@host]
ip  :  10.1.7.71
port  :  6500

[.@host]
ip  :  10.1.7.65
port  :  6500

我可以使用:

val = cf.get("db_finding",'cluster_name')

成功了。

但是我怎样才能得到“AT”主机呢?

【问题讨论】:

    标签: python ini


    【解决方案1】:

    有趣的问题.. 花了我一点时间。我喜欢它:D 那里的解决方案不是很强大。

    import ConfigParser
    
    class MultiOrderedDict(dict):
        def __setitem__(self, key, value):
            if key in self and isinstance(value, list):
                self[key].append(value[0])
                return
    
            super(MultiOrderedDict, self).__setitem__(key, value)
    
    config = ConfigParser.ConfigParser(dict_type=MultiOrderedDict)
    config.read(['cluster.conf'])
    print config.items('.@host')
    print config.get('.@host', 'ip').split('\n')
    

    【讨论】:

    • 谢谢!它的工作。如果 ini 有两个部分“[]”,每个部分有两个“.@host”。如何对应?
    • 嗯?再举一个例子,我会试一试。
    猜你喜欢
    • 2015-07-26
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 2017-06-01
    • 2012-10-18
    • 2012-11-18
    • 1970-01-01
    • 2020-06-16
    相关资源
    最近更新 更多