解析字符串中的键值

Parsing key values in string

我有一个从命令行应用程序获取的字符串。它具有以下结构:

-- section1 --
item11|value11
item12|value12
item13

-- section2 --
item21|value21
item22

我想要的是将其解析为字典,以便我可以轻松访问值:

d['section1']['item11']

我已经解决了没有部分且每个键都有值的情况,但否则我会出错。我已经尝试了几件事,但它变得越来越复杂,因为似乎没有任何效果。这就是我现在拥有的:

s="""
item11|value11
item12|value12
item21|value21
"""
d = {}
for l in s.split('\n'):
    print(l, l.split('|'))
    if l != '':
        d[l.split('|')[0]] = l.split('|')[1]

有人可以帮我扩展这个部分案例并且没有值吗?

似乎非常适合标准库中的 ConfigParser 模块:

d = ConfigParser(delimiters='|', allow_no_value=True)
d.SECTCRE = re.compile(r"-- *(?P<header>[^]]+?) *--")  # sections regex
d.read_string(s)

现在您有了一个可以像字典一样访问的对象:

>>> d['section1']['item11']
'value11'
>>> d['section2']['item22']   # no value case
None

Regexes 很擅长:

import re


def parse(data):
    lines = data.split("\n") #split input into lines
    result = {}
    current_header = ""

    for line in lines:
        if line: #if the line isn't empty
            #tries to match anything between double dashes:
            match = re.match(r"^-- (.*) --$", line)
            if match: #true when the above pattern matches
                #grabs the part inside parentheses:
                current_header = match.group(1)
            else:
                #key = 1st element, value = 2nd element:
                key, value = line.split("|")
                #tries to get the section, defaults to empty section:
                section = result.get(current_header, {})
                section[key] = value #adds data to section
                result[current_header] = section #updates section into result
    return result #done.

print parse("""
-- section1 --
item1|value1
item2|value2
-- section2 --
item1|valueA
item2|valueB""")