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 do I print all sub attributes in comma separated format in one line

I am trying to get only attribute name in one horizontal line.

import xml.etree.ElementTree as ET 

data = '''
<job_details>
    <role>
        <name>Vikas</name>
        <salary>$5.95</salary>
        <job_description>Developer</job_description>
    </role>
    <role>
        <name>Dip</name>
        <salary>$7.95</salary>
        <job_description>Backend Developer</job_description>
    </role>
</job_details>
'''

xml_parsing = ET.fromstring(data)
for sub_attrib in xml_parsing[0]:
  print(sub_attrib.tag)

Expected output:

name,salary,job_description

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

>Solution :

create array then .join()

xml_parsing = ET.fromstring(data)
attributes = ",".join(x.tag for x in  xml_parsing[0])
print(attributes)
# name,salary,job_description
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