我試圖在一條水平線上只獲取屬性名稱。
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)
預期輸出:
name,salary,job_description
uj5u.com熱心網友回復:
然后創建陣列.join()
xml_parsing = ET.fromstring(data)
attributes = ",".join(x.tag for x in xml_parsing[0])
print(attributes)
# name,salary,job_description
uj5u.com熱心網友回復:
使用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,$5.95,Developer
Dip,$7.95,Backend Developer
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/471873.html
上一篇:設計模式七大原則—迪米特法則
