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

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

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 :

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

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']
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