使用Python将子节点输出到CSV

使用Python将子节点输出到CSV,python,xml,csv,xml-parsing,Python,Xml,Csv,Xml Parsing,编辑:我用真实数据替换了示例XML,并在底部提供了我的代码 我有几个xml文件,其中包含1到10多行以下数据: <?xml version="1.0" encoding="UTF-8"?> <Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateCom

编辑:我用真实数据替换了示例XML,并在底部提供了我的代码

我有几个xml文件,其中包含1到10多行以下数据:

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xenc="http://www.w3.org/2001/04/xmlenc#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 UBL-Invoice-2.0.xsd">
   <cac:LegalMonetaryTotal>
      <cbc:PayableAmount currencyID="DKK">2586.61</cbc:PayableAmount>
   </cac:LegalMonetaryTotal>
   <cac:InvoiceLine>
      <cbc:ID>1</cbc:ID>
      <cbc:InvoicedQuantity unitCode="HUR">1.50</cbc:InvoicedQuantity>
      <cbc:LineExtensionAmount currencyID="DKK">1633.65</cbc:LineExtensionAmount>
   </cac:InvoiceLine>
   <cac:InvoiceLine>
      <cbc:ID>2</cbc:ID>
      <cbc:InvoicedQuantity unitCode="HUR">1.00</cbc:InvoicedQuantity>
      <cbc:LineExtensionAmount currencyID="DKK">952.96</cbc:LineExtensionAmount>
   </cac:InvoiceLine>
</Invoice>
…其中,每个文件的每一行都有一行,加上文件名和总数

这是我的代码:

from os import listdir, path, walk
import xml.etree.ElementTree as ET
import csv

def invoicelines(self):
    filename = path.splitext(path.split(file)[1])[0]
    lineId = root.find('./InvoiceLine/ID').text
    lineQuantity = root.find('./InvoiceLine/InvoicedQuantity').text
    lineAmount = root.find('./InvoiceLine/LineExtensionAmount').text
    payableAmount = root.find('./LegalMonetaryTotal/PayableAmount').text

    row = [
        filename,
        lineId,
        lineQuantity,
        lineAmount,
        payableAmount
        ]

    return row

csvfile = 'output.csv'

def csv_write_header(csvfile):
     with open(csvfile, 'w', newline='') as outfile:
          writer = csv.writer(outfile)
          writer.writerow([
               'filename',
               'lineId',
               'lineQuantity',
               'lineAmount',
               'payableAmount'
               ])

xml_files = []
for root, dirs, files in walk('mypath'):
    for file in files:
        if file.endswith('.xml'):
            xml_files.append(path.join(root, file))
            csv_write_header(csvfile)

for file in xml_files:
    tree = ET.iterparse(file)
    for _, el in tree:
        el.tag = el.tag.split('}', 1)[1] # ignores namespaces
    root = tree.root
    if 'Invoice' in root.tag: # only invoice files
        for e in root.iter('InvoiceLine'):
            with open(csvfile, 'a', newline='') as outfile:
                writer = csv.writer(outfile)
                writer.writerow(invoicelines(e))
如果我只分析上面的文件,我得到的输出是:

filename,lineId,lineQuantity,lineAmount,payableAmount
file1,1,1.50,1633.65,2586.61
file1,1,1.50,1633.65,2586.61
…所以我猜这是我的迭代的结果。

试试下面的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApp2

{

    class Program
    {
        const string FILENAME = @"c:\temp\text.csv";
        static void Main()
        {

            string[] filenames = Directory.GetFiles(@"c:\temp", "*.xml");
            StreamWriter writer = new StreamWriter(FILENAME);

            foreach (string filename in filenames)
            {
                XDocument doc = XDocument.Load(filename);

                string amount = (string)doc.Descendants("TotalAmount").FirstOrDefault();

                foreach (XElement line in doc.Descendants("Line"))
                {
                    writer.WriteLine(string.Join(",",
                        filename,
                        (string)line.Element("LineID"),
                        (string)line.Element("Quantity"),
                        (string)line.Element("LineAmount"),
                        amount));
                }
            }
            writer.Flush();
            writer.Close();
        }
    }


}

以下代码实现了您想要的结果。

import os
import xml.etree.ElementTree as ET


def extract_line_id_data(line_element):
    line_id = line_element[0].text
    quantity = line_element[1].text
    line_amount = line_element[2].text

    return line_id, quantity, line_amount


# Iterate over all files in a directory
for _, dirs, files in os.walk('/path/to_folder/with/xml_files/'):
    with open('output.csv', 'a') as output:
        output.write('Filename,LineID,Quantity,LineAmount,TotalAmount\n')  # Headers
        for xml_file in files:
            # If not all files in the folder files are XML you'll need to catch an exception here
            tree = ET.parse(xml_file)  # might need to use os.path.abspath
            root = tree.getroot()
            total_amount = root[0][0].text  # Get total amount value
            # Iterate over all "Line" elements
            for e in root[1:]:
                output.write('{},{},{},{},{}\n'.format(xml_file, * extract_line_id_data(e), total_amount))
使用您的文件和一个“file2.xml”进行测试,该文件的
TotalAmount
为350,输出如下所示:

Filename,LineID,Quantity,LineAmount,TotalAmount
file.xml,1,4,132,407
file.xml,2,1,72,407
file.xml,3,7,203,407
file2.xml,1,4,132,350
file2.xml,2,1,72,350
file2.xml,3,7,203,350

我希望这对你有用。我优先使用ElementTree,尽管我自己也会使用
lxml

TotalAmount在每个文件的开头设置一次?是的。如果出现问题,可以省略,因为我主要对行感兴趣。这是一些看起来很时髦的Python;-)谢谢你的回答。我不断地在函数中出现超出范围的错误,因此我编辑了这些行以包含元素的路径,例如root.find(“./Line/LineID”),但现在它只返回第一个元素的值,因此我的输出如下所示:file.xml、1,4132407、file.xml、1,4132407等。(正确的行数,但具有第一行的输出)。希望它有意义@我需要更多的代码来理解这个问题,考虑编辑你的问题并把它添加进去。我现在已经编辑了我的问题。非常感谢您抽出时间。
Filename,LineID,Quantity,LineAmount,TotalAmount
file.xml,1,4,132,407
file.xml,2,1,72,407
file.xml,3,7,203,407
file2.xml,1,4,132,350
file2.xml,2,1,72,350
file2.xml,3,7,203,350