简体   繁体   English

使用Python(Pandas)将文件夹中的所有xml文件附加到单个Dataframe

[英]Appending all xml files in a folder to single Dataframe using Python (Pandas)

I have a set of xml files in a folder that I am trying to convert to csv and later append them to one Dataframe. 我在试图转换为csv的文件夹中有一组xml文件,后来将它们附加到一个Dataframe中。 The code below helps me to convert xml file to csv. 下面的代码可以帮助我将xml文件转换为csv。 The problem I have however is only the first file gets converted to csv and not the remaining files. 但是,我的问题是只有第一个文件转换为csv,而不是其余文件。 Could anyone guide as to where am I going wrong in the below code: 谁能在下面的代码中指导我哪里出错了:

for file in allFiles:
    print(file)
    def iter_docs(file):
        for docall in file:
            doc_dict = {}
            for doc in docall:
                tag = [elem.tag for elem in doc]
                txt = [elem.text for elem in doc]
                if len(tag) > 0:
                    doc_dict.update(dict(zip(tag, txt)))
                    else:
                        doc_dict[doc.tag] = doc.text
                    yield doc_dict
     etree = ET.parse(file_)
     df_0 = pd.DataFrame(list(iter_docs(etree.getroot())))
     df_0.to_csv("file.csv", index=False)

Create the DataFrame df_0 appending all your data in the xml files and then save to csv file: 创建DataFrame df_0将所有数据附加到xml文件中,然后保存到csv文件中:

df_0 = pd.DataFrame()    # Create df to store all your data
for file in allFiles:        
    print(file)
    def iter_docs(file):
        for docall in file:
            doc_dict = {}
            for doc in docall:
                tag = [elem.tag for elem in doc]
                txt = [elem.text for elem in doc]
                if len(tag) > 0:
                    doc_dict.update(dict(zip(tag, txt)))
                    else:
                        doc_dict[doc.tag] = doc.text
                    yield doc_dict
     etree = ET.parse(file_)
     df_0 = df_0.append(pd.DataFrame(list(iter_docs(etree.getroot()))))    # Append data
df_0.to_csv("file.csv", index=False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Python将文件夹中的所有.xls文件附加到一个.csv文件中 - Appending all .xls files in a folder into one .csv file using Python 在 python 中解析多个 xml 文件并将数据附加到 Python DataFrame - Parsing multiple xml files in python and appending the data to a Python DataFrame 循环浏览文件夹中的所有文件,并使用Python将随机数附加到每个文件的每一行中 - Looping through all files in a folder and appending a random number to each row in each file using Python 将DataFrame追加到Pandas,Python中的列表中 - Appending DataFrame to List in Pandas, Python 将具有多个 excel 文件和多个选项卡的文件夹中的所有电子邮件提取到 pandas dataframe 中 Z23EEEB4347BDD2556DZ3EEEB4347BDD256BDZ - Extract all emails from a folder with multiple excel files and multiple tabs into a pandas dataframe in python python解析/处理文件夹中的所有xml文件 - python parse/process all xml files in folder 将 Excel 文件附加到 Python DataFrame - Appending Excel files to Python DataFrame 有没有更有效的方法将 XML 文件的目录转换为单个 Pandas Dataframe? - Is there a more efficient way to convert a directory of XML Files to a single Pandas Dataframe? How to get all relevant fields from a XML file into a pandas dataframe in Python using xml.etree.ElementTree? - How to get all relevant fields from a XML file into a pandas dataframe in Python using xml.etree.ElementTree? 读取多个 JSON 文件并使用 Python / Pandas 附加到数据集 - Reading multiple JSON files and appending to a dataset using Python / Pandas
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM