Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Regex convert matches to a dictionary [duplicate]

Tags:

python

regex

text = "Bob|19|01012017"
pat = re.compile("(?P<name>.+)|.*|(?P<bday>.+)") #hopefully this regex is correct
result = pat.match(text)
d = result.groupdict()
print d

What I get for d is:

{'bday': None, 'name': 'Bob|19|01012017'}

What I want is:

{bday: "01012017", name: "Bob"}

Can someone point out what I am doing wrong? I only need two fields for dict so I didn't write the age part.

like image 997
Hanming Zeng Avatar asked Dec 14 '22 20:12

Hanming Zeng


1 Answers

You need to escape | to match literally otherwise the pattern is interpreted as or:

text = "Bob|19|01012017"
pat = re.compile("(?P<name>.+)\|.*\|(?P<bday>.+)") 
result = pat.match(text)
d = result.groupdict()

d
# {'bday': '01012017', 'name': 'Bob'}

A quick test against split method on the speed:

text = "Bob|19|01012017"
pat = re.compile("(?P<name>.+)\|.*\|(?P<bday>.+)")
​
def regex_to_dict(texts, pat):
    return [pat.match(text).groupdict() for text in texts]

regex_to_dict([text], pat)
# [{'bday': '01012017', 'name': 'Bob'}]

def split_to_dict(texts):
    dd = []
    for text in texts:
        name, _, bday = text.split('|')
        dd.append({'bday': bday, 'name': name})
    return dd

split_to_dict([text])
# [{'bday': '01012017', 'name': 'Bob'}]

texts = [text] * 100000

%timeit regex_to_dict(texts, pat)
# 10 loops, best of 3: 119 ms per loop

%timeit split_to_dict(texts)
# 10 loops, best of 3: 58.6 ms per loop
like image 97
Psidom Avatar answered Dec 31 '22 00:12

Psidom