如何使用Python将大型xml文件转换为csv

如何使用Python将大型xml文件转换为csv,python,xml-parsing,Python,Xml Parsing,我想将一个非常大的XML文件转换成CSV格式,而不需要硬编码标记名 有人能帮我吗?首先,您需要解析XML文件。这可以通过以下方式完成: 示例代码: import xml.etree.ElementTree as ET root = ET.parse('your_data.xml').getroot() with open("output.csv", "w") as file: for child in root: print(child.tag, child.attrib

我想将一个非常大的XML文件转换成CSV格式,而不需要硬编码标记名


有人能帮我吗?

首先,您需要解析XML文件。这可以通过以下方式完成:

示例代码:

import xml.etree.ElementTree as ET
root = ET.parse('your_data.xml').getroot()
with open("output.csv", "w") as file:
    for child in root:
        print(child.tag, child.attrib)
        # naive example how you could save to csv line wise
        file.write(child.tag+";"+child.attrib)


然后可以将字典另存为CSV。

首先,需要解析XML文件。这可以通过以下方式完成:

示例代码:

import xml.etree.ElementTree as ET
root = ET.parse('your_data.xml').getroot()
with open("output.csv", "w") as file:
    for child in root:
        print(child.tag, child.attrib)
        # naive example how you could save to csv line wise
        file.write(child.tag+";"+child.attrib)


然后可用于将词典另存为CSV。

如果没有关于XML文件结构的任何附加信息,则您的问题没有合理的答案。如果没有关于XML文件结构的任何附加信息,则您的问题没有合理的答案。root=ET.parse('your_data.XML')。getroot()这对于非常大的XML文件失败,因为它们无法加载到memoryroot=ET.parse('your_data.XML')。getroot()对于非常大的XML文件失败,因为它们无法加载到内存中