python regex find line1 and value after line 1, find line2 and value after line2...etc -


sorry wording of questions little confusing...

basically have document following... need use regex name , year each of these chunks dictionary

.... .... .... * name: (name1) * * ...          * * ...          * * year: (year1) * .... .... .... * name: (name2) * * ...          * * ...          * * year: (year2) * .... .... .... * name: (name3) * * ...          * * ...          * * year: (year3) * 

need:

{'name1':'year1','name2':'year2','name3':'year3'} 

you can use following regular expression.

>>> import re >>> regex  = re.compile(r'(?s)name:\s*\((\w+)\).*?year:\s*\((\w+)\)') >>> mydict = dict(re.findall(regex, data)) 

working demo


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -