jeudi 7 juillet 2016

Parsing and creating a dict python


I would like to create a dict by parsing a string

<brns ret = "Herld" other = "very">
<brna name = "ame1">

I would like to create a dict that has the following key-value pairs:

dict = {'brnsret': 'Herld', 
        'brnsother':'very',
        'brnaname':'ame1'}

I have a working script that can handle this:

<brns ret = "Herld">
<brna name = "ame1">

my Code to generate the dict:

match_tag = re.search('<(w+)s(w+) = "(w+)">', each_par_line)
if match_tag is not None:


   dict_tag[match_tag.group(1)+match_tag.group(2)] = match_tag.group(3)

But how should I tweak my script to handle more than one attribute pair in a tag?

Thanks


Aucun commentaire:

Enregistrer un commentaire