需要帮助来编写正确的解析选项

时间:2019-02-10 18:06:41

标签: python regex python-2.7 parsing

我需要解析如下文本文件:

key : 123             
anotherKey : qwer                

oneMoreKey :         
somestring,        
somestring             

这种类型的字符串很多,它们是由服务器自动生成的,所以我不知道有多少字符串会收到解析器

我已经解决了foo:bar这样的解析:

def main():
    data = {}
    file = open('file.txt')  # opening log file
    for line in file:
        if re.match(r'^\s*$', line):
            pass
        else:
            line = line.split(':')

        key = line[0].strip() 
        if len(line) == 2: # this is a workaround for lines like "foo :\n
            value = line[1].strip()
        else:
            value = 'none'

        if key in data:
            pass
        else:
            data[key] = value 

我需要像这样在json中获取所有数据

{
    key : 123,
    anotherKey : qwer,
    oneMoreKey : [somestring, somestring]
}

1 个答案:

答案 0 :(得分:0)

这样吗?

import re

rx = re.compile(r'^(?P<key>\w+)\s:(?P<value>.+?)(?=^\w+\s*:|\Z)', re.S | re.M)

junk = """key : 123             
anotherKey : qwer                
foo : bar, zxc
oneMoreKey :         
somestring,        
somestring         


"""


def match(m):
    values = [val for value in re.split(r', *[\n\r]+', m) for val in [value.strip()] if val]
    return values if len(values) > 1 else m.strip()

d = {m.group('key'): match(m.group('value')) for m in rx.finditer(junk)}
print(d)

这产生

{'key': '123', 'anotherKey': 'qwer', 'foo': 'bar, zxc', 'oneMoreKey': ['somestring', 'somestring']}

请参见a demo on regex101.com

相关问题