我目前正在使用Python通过ElementTree
库和XPath将XML文件转换为CSV格式。如果第一个父母标签的所有子标签(名字,姓氏和地址)都存在,那么我的代码将起作用,但是我收到一条错误消息
儿童索引超出范围
当第一个人缺少子标签时(只有名字和姓氏存在)。
我可以编写哪些代码来绕过此错误消息?这是我第一次使用XPath,如何向其中添加if语句?还是我应该使用其他东西?
这是我的XML文件的样子:
<?xml version="1.0" encoding="utf-8"?>
<Members>
<Person>
<FirstName>JANE</FirstName>
<LastName>DOE</LastName>
</Person>
<Person>
<FirstName>JOHN</FirstName>
<LastName>DOE</LastName>
<Address>
<Address1>123 Straw Street</Address1>
<Address2></Address2>
<City>Apple</City>
<State>Test</State>
<ZipCode>123456 </ZipCode>
</Address>
</Person>
</Members>
当前的Python代码:
import csv
import xml.etree.ElementTree as ET
tree = ET.parse("TestStack.xml")
root = tree.getroot()
xml_data_to_csv =open('OutputStack.csv','w')
Csv_writer=csv.writer(xml_data_to_csv)
list_head=[]
count=0
for element in root.findall('Person'):
person = []
#Get head by tag
if count == 0:
FirstName = element.find('FirstName').tag
list_head.append(FirstName)
LastName = element.find('LastName').tag
list_head.append(LastName)
Address = element[2].tag
list_head.append(Address)
Csv_writer.writerow(list_head)
count = count +1
#get child node
FirstName = element.find('FirstName').text
person.append(FirstName)
LastName = element.find('LastName').text
person.append(LastName)
person.append([e.text for e in element.findall('Address//')])
#Write List_nodes to csv
Csv_writer.writerow(person)
xml_data_to_csv.close()
答案 0 :(得分:1)
我认为标题应该是预定义的。我怀疑您的CSV导入后端是否接受任何格式。
import csv
import xml.etree.ElementTree as ET
tree = ET.parse("in.xml")
root = tree.getroot()
xml_data_to_csv =open('out.csv','w')
Csv_writer=csv.writer(xml_data_to_csv)
list_head=['FirstName', 'LastName', 'Address']
Csv_writer.writerow(list_head)
for element in root.findall('Person'):
person = []
#get child node
FirstName = element.find('FirstName').text
person.append(FirstName)
LastName = element.find('LastName').text
person.append(LastName)
person.append([e.text for e in element.findall('Address//')])
#Write List_nodes to csv
Csv_writer.writerow(person)
xml_data_to_csv.close()
否则,您可以处理异常或检查存在多少这样的元素
if len(element) > 2:
head_list.append(element[2])
continue
预先初始化head_list = [None] * 3 #len(element)
,并在for循环期间收集标头(作为显示)和人员,然后在末尾编写所有内容。我不推荐那条路线。