สมมติว่าเรามีเอกสาร XML ตามอำเภอใจดังต่อไปนี้
<?xml version="1.0" encoding="UTF-8"?>
<programs xmlns="http://something.org/schema/s/program">
<program xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://something.org/schema/s/program http://something.org/schema/s/program.xsd">
<orgUnitId>Organization 1</orgUnitId>
<requiredLevel>academic bachelor</requiredLevel>
<requiredLevel>academic master</requiredLevel>
<programDescriptionText xml:lang="nl">Here is some text; blablabla</programDescriptionText>
<searchword xml:lang="nl">Scrum master</searchword>
</program>
<program xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://something.org/schema/s/program http://something.org/schema/s/program.xsd">
<requiredLevel>bachelor</requiredLevel>
<requiredLevel>academic master</requiredLevel>
<requiredLevel>academic bachelor</requiredLevel>
<orgUnitId>Organization 2</orgUnitId>
<programDescriptionText xml:lang="nl">Text from another organization about some stuff.</programDescriptionText>
<searchword xml:lang="nl">Excutives</searchword>
</program>
<program xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<orgUnitId>Organization 3</orgUnitId>
<programDescriptionText xml:lang="nl">Also another huge text description from another organization.</programDescriptionText>
<searchword xml:lang="nl">Negotiating</searchword>
<searchword xml:lang="nl">Effective leadership</searchword>
<searchword xml:lang="nl">negotiating techniques</searchword>
<searchword xml:lang="nl">leadership</searchword>
<searchword xml:lang="nl">strategic planning</searchword>
</program>
</programs>
ขณะนี้ฉัน looping
อยู่เหนือองค์ประกอบที่ฉันต้องการโดยใช้เส้นทางสัมบูรณ์ เนื่องจากฉันไม่สามารถใช้วิธีการ get
หรือ find
ใด ๆ ใน ElementTree ได้ ดังนั้นโค้ดของฉันจึงมีลักษณะดังนี้:
import pandas as pd
import xml.etree.ElementTree as ET
import numpy as np
import itertools
tree = ET.parse('data.xml')
root = tree.getroot()
root.tag
dfcols=['organization','description','level','keyword']
organization=[]
description=[]
level=[]
keyword=[]
for node in root:
for child in
node.findall('.//{http://something.org/schema/s/program}orgUnitId'):
organization.append(child.text)
for child in node.findall('.//{http://something.org/schema/s/program}programDescriptionText'):
description.append(child.text)
for child in node.findall('.//{http://something.org/schema/s/program}requiredLevel'):
level.append(child.text)
for child in node.findall('.//{http://something.org/schema/s/program}searchword'):
keyword.append(child.text)
แน่นอนว่าเป้าหมายคือการสร้างดาต้าเฟรมเดียว อย่างไรก็ตาม เนื่องจากแต่ละโหนดในไฟล์ XML มีองค์ประกอบหนึ่งหรือหลายองค์ประกอบ เช่น requiredLevel
หรือ searchword
ขณะนี้ฉันกำลังสูญเสียข้อมูลเมื่อฉันส่งไปยัง dataframe โดยวิธีใดวิธีหนึ่งต่อไปนี้
df=pd.DataFrame(list(itertools.zip_longest(organization,
description,level,searchword,
fillvalue=np.nan)),columns=dfcols)
หรือใช้ pd.Series
ตามที่กำหนด ที่นี่ หรือวิธีแก้ไขปัญหาอื่นที่ฉันไม่ทำ ดูเหมือนจะไม่พอดีจากที่นี่
ทางออกที่ดีที่สุดของฉันคือไม่ใช้ Lists เลย เนื่องจากดูเหมือนว่าจะจัดทำดัชนีข้อมูลไม่ถูกต้อง นั่นคือฉันสูญเสียข้อมูลจากโหนดย่อยที่ 2 ถึง X แต่ตอนนี้ฉันติดอยู่ และไม่เห็นตัวเลือกอื่นเลย
ผลลัพธ์สุดท้ายของฉันควรมีลักษณะดังนี้:
organization description level keyword
Organization 1 .... academic bachelor, Scrum master
academic master
Organization 2 .... bachelor, Executives
academic master,
academic bachelor
Organization 3 .... Negotiating,
Effective leadership,
negotiating techniques,
....