【发布时间】:2019-12-01 02:39:39
【问题描述】:
我有一个 xml,我想将其转换为 csv,但出现错误。
在我的 xml 文件中,我希望只将选定的列写入 csv。
import xml.etree.ElementTree as ET
import pandas as pd
root = ET.parse('D:\\Task\\09_ActionRecorder_0.XML').getroot()
tags =[]
for elem in root:
for child in elem:
try:
tag = {}
tag["TL"] = child.attrib['TL']
tag["CN"] = child.attrib['CN']
tag["DT"] = child.attrib['DT']
tag["AN"] = child.attrib['AN']
tags.append(tag)
except KeyError:
tags.append(tag)
print(tags)
df_users = pd.DataFrame(tags)
#df_users.head(20)
column_name_update = df_users.rename(columns = {"TL": "Title",
"CN":"Control Name",
"DT": "Date Time",
"AN": "Application Name"})
#new_data.head(20)
column_name_update.to_csv("D:\\Tasks\\Sample.csv",index=False, columns=["Title", 'Control Name', 'Date Time', 'Application Name'])
从给定的 xml 文件中,我希望只写入有限的列(如代码所示)。但每当我执行上面的代码时,我都会遇到关键错误,并且在 csv 文件中只有一列被写入。请帮助如果任何人都知道该怎么做。
【问题讨论】:
标签: python xml csv file-conversion