如何在一行中以逗号分隔格式打印所有子属性

How do I print all sub attributes in comma separated format in one line

我试图在一条水平线上只获取属性名称。

import xml.etree.ElementTree as ET 

data = '''
<job_details>
    <role>
        <name>Vikas</name>
        <salary>.95</salary>
        <job_description>Developer</job_description>
    </role>
    <role>
        <name>Dip</name>
        <salary>.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)

预期输出:

name,salary,job_description

然后创建数组 .join()

xml_parsing = ET.fromstring(data)
attributes = ",".join(x.tag for x in  xml_parsing[0])
print(attributes)
# name,salary,job_description

使用 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',','))

输出:

Vikas,.95,Developer
Dip,.95,Backend Developer