'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
Solution 1:[1]
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
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | uingtea |
| Solution 2 |
