将列表列表转换为具有特殊情况的字典列表

时间:2015-01-29 21:55:51

标签: list python-3.x dictionary

我的列表包含这样的元素:

[ [', , PropertyName, com.bs.ConsoleLogLevel, ', ', , Type, Integer, ', ', , Default, 3, ', ', , Description, "The logging level for outputting to console. (1 - Critical, 2 - Warning, 3 - Info, 4 - Debug, 5 - Trace)", ', ', , Sample, com.bs.ConsoleLogLevel=5, ', ', , DependsOn, com.rim.bs.ConsoleLogEnable, ', ', , Min, 1, ', ', , Max, 5, '], 

[', , PropertyName, com.rim.bs.SyslogProtocol, ', ', , Type, String, ', ', , Default, tcp, ', ', , Description, "The supported protocols are:', ' 1. udp -- The original syslog UDP protocol, pointed to 104.0.0.21 (localhost)', ' 2. tcp -- An implementation of the de-facto TCP/IP syslog protocol, which points to a TCP-capable syslog server, defaulted to 104.0.021 (localhost).', ' 3. unix_syslog -- Native syslog support for Unix platforms', ' 4. unix_socket -- Native socket support for Unix platforms, pointed to the "/var/log" socket", ', ', , Sample, com.bs.SyslogProtocol=tcp, ', ', , DependsOn, com.rim.bs.SyslogEnable, '] ]

I want an output like this (list of dictionaries): 

[ {PropertyName : com.bs.ConsoleLogLevel, Type : Integer, Default : 3, Description: "The logging level for outputting to console. (1 - Critical, 2 - Warning, 3 - Info, 4 - Debug, 5 - Trace)", Sample : com.bs.ConsoleLogLevel=3, DependsOn : com.bs.ConsoleLogEnable, Min : 1, Max : 5 }, {PropertyName : com.bs.SyslogProtocol, Type:  String, Default : tcp, Description : "The supported protocols are:', ' 1. udp -- The original syslog UDP protocol, pointed to 104.0.0.2 (localhost)', ' 2. tcp -- An implementation of the de-facto TCP/IP syslog protocol, which points to a TCP-capable syslog server, defaulted to 104.0.0.2 (localhost).', ' 3. unix_syslog -- Native syslog support for Unix platforms', ' 4. unix_socket -- Native socket support for Unix platforms, pointed to the "/var/log/soc" socket", Sample : com.bs.SyslogProtocol=tcp,  DependsOn : com.bs.SyslogEnable } ]

代码:

fdcit = {}

对于outlist中的项目:

   #glist = [] 

   print ("=====================") 

   for listitem in item:

       fdict [listitem.split(", ,")[1]] = listitem.split(", ,")[2:]

       `enter code here`print (fdict)`

问题:代码在访问此行时会中断:1 tcp ... 2. udp ...依此类推。我是python的新手。请帮忙。

1 个答案:

答案 0 :(得分:0)

你的例子可以这样处理:

li=[ [', , PropertyName, com.bs.ConsoleLogLevel, ', ', , Type, Integer, ', ', , Default, 3, ', ', , Description, "The logging level for outputting to console. (1 - Critical, 2 - Warning, 3 - Info, 4 - Debug, 5 - Trace)", ', ', , Sample, com.bs.ConsoleLogLevel=5, ', ', , DependsOn, com.rim.bs.ConsoleLogEnable, ', ', , Min, 1, ', ', , Max, 5, '], 

    [', , PropertyName, com.rim.bs.SyslogProtocol, ', ', , Type, String, ', ', , Default, tcp, ', ', , Description, "The supported protocols are:', ' 1. udp -- The original syslog UDP protocol, pointed to 104.0.0.21 (localhost)', ' 2. tcp -- An implementation of the de-facto TCP/IP syslog protocol, which points to a TCP-capable syslog server, defaulted to 104.0.021 (localhost).', ' 3. unix_syslog -- Native syslog support for Unix platforms', ' 4. unix_socket -- Native socket support for Unix platforms, pointed to the "/var/log" socket", ', ', , Sample, com.bs.SyslogProtocol=tcp, ', ', , DependsOn, com.rim.bs.SyslogEnable, '] ]


result=[]  
for l in li:
    fdict={}
    for e in l:
        if e.startswith(', ,'):
            e=e.lstrip(', ,')
            e=e.rstrip().rstrip(',')
            k, sep, v=e.partition(',')
            fdict[k]=v.strip()
        else:
            fdict['Description']+=e  
    result.append(fdict)          
print(result)       

打印:

[{'Description': '"The logging level for outputting to console. (1 - Critical, 2 - Warning, 3 - Info, 4 - Debug, 5 - Trace)"', 'PropertyName': 'com.bs.ConsoleLogLevel', 'Default': '3', 'Min': '1', 'Type': 'Integer', 'DependsOn': 'com.rim.bs.ConsoleLogEnable', 'Sample': 'com.bs.ConsoleLogLevel=5', 'Max': '5'}, {'Description': '"The supported protocols are: 1. udp -- The original syslog UDP protocol, pointed to 104.0.0.21 (localhost) 2. tcp -- An implementation of the de-facto TCP/IP syslog protocol, which points to a TCP-capable syslog server, defaulted to 104.0.021 (localhost). 3. unix_syslog -- Native syslog support for Unix platforms 4. unix_socket -- Native socket support for Unix platforms, pointed to the "/var/log" socket", ', 'PropertyName': 'com.rim.bs.SyslogProtocol', 'Default': 'tcp', 'Type': 'String', 'DependsOn': 'com.rim.bs.SyslogEnable', 'Sample': 'com.bs.SyslogProtocol=tcp'}]