python regex: create dictionary from string

穿精又带淫゛_ 提交于 2021-01-29 07:54:34

问题


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?

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.


回答1:


>>> 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'}



回答2:


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

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'}
]


来源:https://stackoverflow.com/questions/56240304/python-regex-create-dictionary-from-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!