1
votes

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
2

2 Answers

1
votes

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
1
votes

Using bs4.

from bs4 import BeautifulSoup

soup = BeautifulSoup(data,'html.parser')

out = soup.text.strip().split('\n\n\n')

for a in out:
    print(a.replace('\n',','))

OUTPUT:

Vikas,$5.95,Developer
Dip,$7.95,Backend Developer