简体   繁体   English

将 Python XML ElementTree 输出写入 CSV

[英]Writing Python XML ElementTree output to CSV

TL;DR I'm now able to output the information I want in the CSV but I'm just repeating the last XML file's data over and over again. TL;DR 我现在可以在 CSV 中输出我想要的信息,但我只是一遍又一遍地重复最后一个 XML 文件的数据。

This is the latest version of the script:这是脚本的最新版本:

import csv
import glob
import xml.etree.ElementTree as ET
filenames = glob.glob("..\Lib\macros\*.xml")

for filename in filenames:

  with open(filename, 'r') as content:
    element = ET.parse(content)
    root = element.getroot()
    print(root.attrib, filename)
  e = element.findall('commands/MatrixSwitch/')
  for i in e:
    print (i.tag, i.text)


    with open('results.csv', 'w', newline='') as file:
        for filename in filenames:
            writer = csv.writer(file)
            writer.writerow([root.attrib, filename])
            for i in e:
                writer.writerow([i.tag, i.text])

Say I have 10 XML files, I'm getting the output related to XML "File 10" 10 times in the CSV, not anything for XML "File 1-9" ... sure its something simple?假设我有 10 个 XML 文件,我在 CSV 中得到 10 次与 XML“文件 10”相关的输出,而不是 XML“文件 1-9”的任何内容......确定它很简单吗?

========================================================================= ================================================== ========================

I've written a small script which ingests a folder of XML files, searches for a particular element and then recalls some of the data.我编写了一个小脚本,它接收一个包含 XML 文件的文件夹,搜索特定元素,然后调用一些数据。 This is then printed to the console and written to a CSV, except I'm having trouble formatting my CSV correctly.然后将其打印到控制台并写入 CSV,除非我无法正确格式化我的 CSV。

This is where I've got so far:这是我到目前为止的地方:

import csv
import glob
import xml.etree.ElementTree as ET
filenames = glob.glob("..\Lib\macros\*.xml")

for filename in filenames:

  with open(filename, 'r') as content:
    element = ET.parse(content)
    root = element.getroot()
    print(root.attrib, filename)
  e = element.findall('commands/MatrixSwitch/')
  for i in e:
    print (i.tag, i.text)
  with open('results.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow([root.attrib, filename])

I'm looking to capture the following data:我希望捕获以下数据:

  • XML Filename XML 文件名
  • Macro Name宏名称
  • Monitor ID监视器 ID
  • Camera ID相机 ID

I'm only interested in the and when a "Matrix Switch" is referred to in the XML.我只对 XML 中何时引用“矩阵开关”感兴趣。 Sometimes there might only be one monitor ID and one camera ID, sometimes there might be more so the script needs to loop through and get all of the IDs within the "Matrix Switch" element.有时可能只有一个监视器 ID 和一个摄像机 ID,有时可能会有更多,因此脚本需要遍历并获取“矩阵切换”元素中的所有 ID。 This seems to work so far.到目前为止,这似乎有效。

Typical XML structure looks like this :典型的 XML 结构如下所示:

<macro name="NAME OF THE MACRO IS SHOWN HERE">
<execution>
<delay>0</delay>
</execution>
<parameters/>
<commands>
<MatrixSwitch>
<camera>1530</camera>
<monitor>1020</monitor>
</MatrixSwitch>
<MatrixSwitch>
<camera>1531</camera>
<monitor>1001</monitor>
</MatrixSwitch>
</commands>
</macro>

Or like this :或者像这样:

<macro name="ANOTHER NAME GOES HERE">
<execution>
<delay>0</delay>
</execution>
<parameters/>
<commands>
<MatrixSwitch>
<camera>201</camera>
<monitor>17</monitor>
</MatrixSwitch>
<MatrixSwitch>
<camera>206</camera>
<monitor>18</monitor>
</MatrixSwitch>
<MatrixSwitch>
<camera>202</camera>
<monitor>19</monitor>
</MatrixSwitch>
<MatrixSwitch>
<camera>207</camera>
<monitor>20</monitor>
</MatrixSwitch>
</commands>
</macro>

My current results.csv is only set to output the name and filename.我当前的 results.csv 仅设置为输出名称和文件名。 This works but I'm unsure where I need to add the "writer" command to the loop where its dealing with the Monitor ID and Camera ID .这有效,但我不确定我需要在哪里将“writer”命令添加到循环中,在那里它处理 Monitor ID 和 Camera ID 。

I want my CSV to show : Name, Filename, Monitor A, Camera A, Monitor B, Camera B, Monitor C, Camera C, Monitor D, Camera D etc.....我希望我的 CSV 显示:名称、文件名、监视器 A、摄像机 A、监视器 B、摄像机 B、监视器 C、摄像机 C、监视器 D、摄像机 D 等.....

Any pointers greatly appreciated!!任何指针都非常感谢!!

Code has now been changed slightly :代码现在略有更改:

import csv
import glob
import xml.etree.ElementTree as ET
filenames = glob.glob("..\Lib\macros\*.xml")

for filename in filenames:

  with open(filename, 'r') as content:
    element = ET.parse(content)
    root = element.getroot()
    print(root.attrib, filename)
  e = element.findall('commands/MatrixSwitch/')
  for i in e:
    print (i.tag, i.text)
    with open('results.csv', 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow([root.attrib, filename])
        for i in e:
           writer.writerow([i.tag, i.text])

Output in the CSV is as below : CSV 中的输出如下:

https://imgur.com/a/SrPrgjm https://imgur.com/a/SrPrgjm

Just add a loop calling writerow:只需添加一个循环调用 writerow:

...
with open('results.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow([root.attrib, filename])
    for i in e:
        writer.writerow([i.tag, i.text])

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM