繁体   English   中英

Python解析字符串列表

[英]Python parsing list of string

我有字符串列表,我正在寻找像这样的行:

密钥:af12d9索引:​​0字段1:1234字段2:1234字段3:-10

找到这样的行后,我想将每个字典存储为字典{'key':af12d9,'index':0,'field 1':....},然后将该字典存储到列表中,所以我将有字典清单。

我能够使它像这样工作:

listconfig = []
for line in list_of_strings:
    matched = findall("(Key:[\s]*[0-9A-Fa-f]+[\s]*)|(Index:[\s]*[0-9]+[\s]*)|(Field 1:[\s]*[0-9]+[\s]*)|(Field 2:[\s]*[0-9]+[\s]*)|(Field 3:[\s]*[-+]?[0-9]+[\s]*)", line)
    if matched:
        listconfig += [dict(map(lambda pair: (pair[0].strip().lower(), pair[1].strip().lower()),
                                map(lambda line: line[0].split(':'),
                                    [filter(lambda x: x, group) for group in matched])))]

我只是想知道是否有更好的方法(简短高效)来执行此操作,因为我认为findall将对每个字符串执行5次搜索。 (正确吗?因为它返回5个元组的列表。)

谢谢。

解:

好的,在brandizzi的帮助下,我找到了该问题的答案。

解:

listconfig = []
for line in list_of_strings:
    matched = re.search(r"Key:[\s]*(?P<key>[0-9A-Fa-f]+)[\s]*" \ 
                        r"(Index:[\s]*(?P<index>[0-9]+)[\s]*)?" \ 
                        r"(Field 1:[\s]*(?P<field_1>[0-9]+)[\s]*)?" \ 
                        r"(Field 2:[\s]*(?P<field_2>[0-9 A-Za-z]+)[\s]*)?" \ 
                        r"(Field 3:[\s]*(?P<field_3>[-+]?[0-9]+)[\s]*)?", line) 
    if matched:
        print matched.groupdict()
        listconfig.append(matched.groupdict())

首先,您的正则表达式似乎无法正常工作。 Key字段应具有可以包含f值,对吗? 因此,其组不应为([0-9A-Ea-e]+) ,而应为([0-9A-Fa-f]+) 同样,在处理正则表达式时,在正则表达式字符串前加上r前缀是一个很好的做法,实际上是一个奇妙的做法,因为它避免了\\转义字符的问题。 (如果您不明白为什么要这样做,请查看原始字符串

现在,我解决问题的方法。 首先,我将创建一个没有管道的正则表达式:

>>> regex = r"(Key):[\s]*([0-9A-Fa-f]+)[\s]*" \
...     r"(Index):[\s]*([0-9]+)[\s]*" \
...     r"(Field 1):[\s]*([0-9]+)[\s]*" \
...     r"(Field 2):[\s]*([0-9 A-Za-z]+)[\s]*" \
...     r"(Field 3):[\s]*([-+]?[0-9]+)[\s]*"

进行此更改后, findall()将只返回整行中找到的组的一个元组。 在此元组中,每个键后面都有其值:

>>> re.findall(regex, line)
[('Key', 'af12d9', 'Index', '0', 'Field 1', '1234', 'Field 2', '1234 Ring ', 'Field 3', '-10')]

所以我得到了元组...

>>> found = re.findall(regex, line)[0]
>>> found
('Key', 'af12d9', 'Index', '0', 'Field 1', '1234', 'Field 2', '1234 Ring ', 'Field 3', '-10')

...并且使用切片我只能得到钥匙...

>>> found[::2]
('Key', 'Index', 'Field 1', 'Field 2', 'Field 3')

...以及仅值:

>>> found[1::2]
('af12d9', '0', '1234', '1234 Ring ', '-10')

然后,我使用zip()函数创建一个包含键及其对应值的元组列表:

>>> zip(found[::2], found[1::2])
[('Key', 'af12d9'), ('Index', '0'), ('Field 1', '1234'), ('Field 2', '1234 Ring '), ('Field 3', '-10')]

结局是将元组列表传递给dict()构造函数:

>>> dict(zip(found[::2], found[1::2]))
{'Field 3': '-10', 'Index': '0', 'Field 1': '1234', 'Key': 'af12d9', 'Field 2': '1234 Ring '}

我认为这种解决方案是最好的,但是从某种意义上说,这确实是一个主观问题。 反正HTH :)

好的,在brandizzi的帮助下,我找到了该问题的答案。

解:

listconfig = []
for line in list_of_strings:
    matched = re.search(r"Key:[\s]*(?P<key>[0-9A-Fa-f]+)[\s]*" \ 
                        r"(Index:[\s]*(?P<index>[0-9]+)[\s]*)?" \ 
                        r"(Field 1:[\s]*(?P<field_1>[0-9]+)[\s]*)?" \ 
                        r"(Field 2:[\s]*(?P<field_2>[0-9 A-Za-z]+)[\s]*)?" \ 
                        r"(Field 3:[\s]*(?P<field_3>[-+]?[0-9]+)[\s]*)?", line) 
    if matched:
        print matched.groupdict()
        listconfig.append(matched.groupdict())
import re

str_list = "Key: af12d9 Index: 0 Field 1: 1234 Field 2: 1234 Ring Field 3: -10"
results = {}
for match in re.findall("(.*?):\ (.*?)\ ", str_list+' '):
    results[match[0]] = match[1]

由于“ Ring”,示例中的模式可能与示例数据不匹配。 以下代码可能会有所帮助:

import re
# the keys to look for
keys = ['Key','Index','Field 1','Field 2','Field 3']
# a pattern for those keys in exact order
pattern = ''.join(["(%s):(.*)" % key for key in keys])
# sample data
data = "Key: af12d9 Index: 0 Field 1: 1234 Field 2: 1234 Ring Field 3: -10"
# look for the pattern
hit = re.match(pattern,data)
if hit:
    # get the matched elements
    groups = hit.groups()
    # group them in pairs and create a dict
    d = dict(zip(groups[::2], groups[1::2]))
    # print result
    print d

您可以使用解析器库。 我知道Lepl,所以会用到它,但是因为它是用Python实现的,所以效率不高。 但是,解决方案很短,我希望它很容易理解:

def parser():
  key = (Drop("Key:") & Regexp("[0-9a-fA-F]+")) > 'key'
  index = (Drop("Index:") & Integer()) > 'index'
  def Field(n):
      return (Drop("Field" + str(n)) & Integer()) > 'field'+str(n)
  with DroppedSpaces():
      line = (key & index & Field(1) & Field(2) & Field(3)) >> make_dict
      return line[:]
p = parser()
print(p.parse_file(...))

处理可变数量的字段也应该相对简单。

请注意,以上内容尚未经过测试(我需要开始工作),但应该是正确的。 特别是,它应根据需要返回字典列表。

如果这样做,您的解决方案将表现更好[*]:

import re

from itertools import imap

regex = re.compile(flags=re.VERBOSE, pattern=r"""
    Key:\s*(?P<key>[0-9A-Fa-f]+)\s*
    Index:\s*(?P<index>[0-9]+)\s*
    Field\s+1:\s*(?P<field_1>[0-9]+)\s*
    Field\s+2:\s*(?P<field_2>[0-9A-Za-z]+)\s*
    Field\s+3:\s*(?P<field_3>[-+]?[0-9]+)\s*
""")

list_of_strings = [
    'Key: af12d9 Index: 0 Field 1: 1234 Field 2: 1234 Field 3: -10',
    'hey joe!',
    ''
]

listconfig = [
    match.groupdict() for match in imap(regex.search, list_of_strings) if match
]

而且,它会更加简洁。 此外,我修复了损坏的正则表达式模式。

顺便说一句,上述结果将是:

[{'index': '0', 'field_2': '1234', 'field_3': '-10', 'key': 'af12d9', 'field_1': '1234'}]

[*]实际上-不,不是。 我对两者都计时,但没有一个比另一个快。 不过,我更喜欢我的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM