简体   繁体   English

python regex:从字符串创建字典

[英]python regex: create dictionary from string

I have a sting containing multiple informations which I want to save in a dictionary: 我有一个字符串,其中包含多种信息,我想保存在字典中:

s1 = "10:12:01    R1 3    E44"
s2 = "11:11:01    R100    E400"

pattern = "\d{2}:\d{2}:\d{2}(\,\d+)?" + \
          " +" + \
          "[0-9A-Za-z _]{2}([0-9A-Za-z _]{1})?([0-9A-Za-z _]{1})?" + \
          " +" + \
          "[0-9A-Za-z _]{2}([0-9A-Za-z _]{1})?([0-9A-Za-z _]{1})?$"

# --> 

d1 = {"time" : "10:12:01",
      "id1" : "R1 3", 
      "id2" : "E44"}

d2 = {"time" : "11:11:01",
      "id1" : "R100", 
      "id2" : "E400"}

is there a way doing this directly with python re? 有没有办法直接用python re做到这一点?

Note: I'm aware that there is a similar question here: regex expression string dictionary python , however the formulation is not precisly pointing to what I expact as answer. 注意:我知道这里有一个类似的问题: regex表达式字符串字典python ,但是表述并没有精确地指向我作为答案的含义。

If the information is cleanly divided by whitespaces, why not use that information to split the string by whitespace and create the resultant list of dictionaries. 如果信息完全由空格分隔,为什么不使用该信息按空格分隔字符串并创建结果字典列表。
If we have multiple whitespaces, we can ignore those whitespaces while splitting using re.split 如果我们有多个空格,则可以在使用re.split进行拆分时忽略这些空格

import re

#List of strings
li = [ "10:12:01    R1 3    E44", "11:11:01    R100    E400"]

#List of kyes
keys = ['time', 'id1', 'id2']

#Create the dictionary from keys from keys listand values obtained by splitting string on 2 or more whitespaces
result = [{keys[idx]:re.split(r'\s{2,}', s)[idx] for idx in range(len(keys))} for s in li]

print(result)

The output will be 输出将是

[
{'time': '10:12:01', 'id1': 'R1 3', 'id2': 'E44'}, 
{'time': '11:11:01', 'id1': 'R100', 'id2': 'E400'}
]
>>> import re
>>> pattern = "(?P<time>\d{2}:\d{2}:\d{2}(\,\d+)?) +(?P<id1>[0-9A-Za-z_]{2}([0-9A-Za-z1-9_]{1})?([0-9A-Za-z_]{1})?) +(?P<id2>[0-9A-Za-z_]{2}([0-9A-Za-z1-9_]{1})?([0-9A-Za-z_]{1})?$)"
>>>
>>> s1 = "10:12:01    R123    E44"
>>> print(re.match(pattern, s1).groupdict())
{'time': '10:12:01', 'id1': 'R123', 'id2': 'E44'}

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

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