How to fill the missing attribute value with null | python |

Advertisements

I am been trying to implement the logic , But not getting how do I start

My xml as below data in which if you see the child root as 3 attributes
If any one of the attribute is missing it should be populated as null in that case

Code :

import xml.etree.ElementTree as ET

xml_data='''
<job_details>
    <role>
        <name>Vilan</name>
        <salary>$5.95</salary>
        <job_description>Developer</job_description>
    </role>
    <role>
        <name>Dils</name>
        <salary>$7.95</salary>
    </role>
    <role>
        <name>Raj</name>
        <job_description>Fullstack Developer</job_description>
    </role>
</job_details>
'''

get_root_element = ET.fromstring(xml_data)

out = []
for item in get_root_element:
    res = ','.join(x.text for x in item)
    out.append(res)

Expected output

['Vilan,$5.95,Developer', 'Dils,$7.95,null' , 'Raj',null,'Fullstack Developer'  ]

>Solution :

Here is how you can do that. You need to define all fields and then just search for them.

EDIT
As in the comments discussed I changed your xml_data in the 3rd role, name field exists now but it doesn`t have a value.

import xml.etree.ElementTree as ET

all_fields = ["name", "salary", "job_description"]

xml_data = """
<job_details>
    <role>
        <name>Vilan</name>
        <salary>$5.95</salary>
        <job_description>Developer</job_description>
    </role>
    <role>
        <name>Dils</name>
        <salary>$7.95</salary>
    </role>
    <role>
        <name></name>
        <job_description>Fullstack Developer</job_description>
    </role>
</job_details>
"""
out = []
get_root_element = ET.fromstring(xml_data)
for item in get_root_element:
    tmp = []
    for fields in all_fields:
        res = item.find(fields)
        if res is not None:
            if res.text is not None:
                tmp.append(res.text)
            else:
                tmp.append("null")
        else:
            tmp.append("null")
    out.append(",".join(tmp))

print(out)

['Vilan,$5.95,Developer', 'Dils,$7.95,null', 'null,null,Fullstack Developer']

Leave a ReplyCancel reply