【问题标题】:Python - Parsing all XML files in a folder to CSV filesPython - 将文件夹中的所有 XML 文件解析为 CSV 文件
【发布时间】:2021-09-16 09:53:45
【问题描述】:

我刚开始学习 python,所以这可能是一个非常基本的问题,但这就是我卡住的地方。

我正在尝试解析给定文件夹中的所有 XML 文件并输出 CSV 文件,文件名与原始 XML 文件相同。我已经用单个文件进行了测试,它工作得很好,但我遇到的问题是对所有文件执行相同的操作,并在循环中运行,因为这将是一个永久脚本。

这是我的代码:

import os
import xml.etree.cElementTree as Eltree
import pandas as pd

path = r'C:/python_test'
filenames = []


for filename in os.listdir(path):
    if not filename.endswith('.xml'): continue
    fullname = os.path.join(path, filename)
    print(fullname)
    filenames.append(fullname)


cols = ["serviceName", "startDate", "endDate"]
rows = []

for filename in filenames:
    xmlparse = Eltree.parse(filename)
    root = xmlparse.getroot()
    csvoutput=[]


for fixed in root.iter('{http://www.w3.org/2001/XMLSchema}channel'):

    channel = fixed.find("channelName").text


for dyn in root.iter('programInformation'):
    
    start = dyn.find("publishedStartTime").text
    end = dyn.find("endTime").text

    rows.append({"serviceName": channel, "startDate": start, "endDate": end})

df = pd.DataFrame(rows, columns=cols)
df.to_csv(csvoutput)

这是我得到的错误:

C:/python_test\1.xml
C:/python_test\2.xml
C:/python_test\3.xml
C:/python_test\4.xml
C:/python_test\5.xml
C:/python_test\6.xml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 49, in <module>
  File "C:\Users\ragehol\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\core\generic.py", line 3466, in to_csv
    return DataFrameRenderer(formatter).to_csv(
  File "C:\Users\ragehol\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\format.py", line 1105, in to_csv
    csv_formatter.save()
  File "C:\Users\ragehol\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\formats\csvs.py", line 237, in save
    with get_handle(
  File "C:\Users\ragehol\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 609, in get_handle
    ioargs = _get_filepath_or_buffer(
  File "C:\Users\ragehol\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 396, in _get_filepath_or_buffer
    raise ValueError(msg)
ValueError: Invalid file path or buffer object type: <class 'list'>

任何形式的建议将不胜感激!

非常感谢!

【问题讨论】:

    标签: python python-3.x pandas xml export-to-csv


    【解决方案1】:

    这是你的错误:

    csvoutput=[] 定义为list。稍后你将它作为参数传递给df.to_csv(csvoutput)。因此,您将 list 传递给查找文件路径的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-11
      • 2016-12-17
      • 2022-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      相关资源
      最近更新 更多