Is it possible to access the symbolic group name defined in a regular expression with (?P<toto>...) with the equivalent of re.findall()?
Using re.match(), re returns a MatchObject on which the function .group('toto') can be used… I would like to do something close.
Here is an example :
import re
my_str = 'toto=1, bip=xyz, toto=15, bip=abu'
print re.findall('toto=(?P<toto>\d+)\,\sbip=(?P<bip>\w+)', my_str)
It returns :
[('1', 'xyz'), ('15', 'abu')]
I would like to get something like :
[{'toto':'1', 'bip':'xyz'}, {'toto':'15', 'bip':'abu'}]
Is there any simple way to do that?
I can’t find it anywhere…
You can’t do that with
.findall(). However, you can achieve the same effect with.finditer()and some list comprehension magic:This prints:
So we loop over each match yielded by
.finditer()and take it’s.groupdict()result.