Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Comma separated names list to parse into dictionary

I’d like to parse a list of assignees into a dictionary field. Creating the dictionary is not a problem since Python re.match() returned value can be grouped into dictionary fields re.match().groupdict().

My regex r'assignees:((\s\w+){2}[\s|,])+' matches as expected with the following example input if I test it in this website:

Input:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

assignees: Peter Jones, Michael Jackson, Tim Burton

However, the same regex pattern in Python only matches Peter Jones, resulting in:

assignees: Peter Jones,

My pattern is declared in a variable p in this way:

p = re.compile(r'assignees:(?P<assignees>((\s\w+){2}[\s|,])+')

The final output I’m aiming for is:

{'assignees': 'Peter Jones, Michael Jackson, Tim Burton'}

Can anyone point out what I’m doing wrong/missing here please?

>Solution :

Your pattern was missing a parentheses and the question has no MRE, but this gives the result I believe you want. If it isn’t satisfactory, make a MRE next time:

import re

s = 'assignees: Peter Jones, Michael Jackson, Tim Burton'
p = re.compile(r'assignees:\s*(?P<assignees>([^,]+(?:,[^,]+)*))')
m = p.match(s)
print(m.groupdict())

Output:

{'assignees': 'Peter Jones, Michael Jackson, Tim Burton'}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading