因为总会有至少一个
TaxTotal
元素,我将为每一行创建一个新的csv行,并为前面的值返回树。
下面是一个使用lxml的例子。我添加了一个函数,使处理空值变得更容易,但我将留给您对值的任何其他格式设置。
蟒蛇3.6
from lxml import etree
import csv
def get_value(target_tree, xpath, namespaces):
try:
return target_tree.xpath(xpath, namespaces=namespaces)[0].text
except IndexError:
return ""
tree = etree.parse("input.xml")
ns = {"cac": "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",
"cbc": "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",
"i2": "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"}
with open("output.csv", "w") as csvfile:
csvwriter = csv.writer(csvfile, delimiter=";", lineterminator="\n", quoting=csv.QUOTE_MINIMAL)
# Header
csvwriter.writerow(["ID", "/InvoiceLine/ID", "/InvoiceLine/InvoicedQuantity", "/InvoiceLine/LineExtensionAmount",
"/InvoiceLine/TaxTotal/TaxAmount", "/InvoiceLine/TaxTotal/TaxSubtotal/TaxableAmount",
"/InvoiceLine/TaxTotal/TaxSubtotal/TaxAmount",
"/InvoiceLine/TaxTotal/TaxSubtotal/TaxCategory/ID",
"/InvoiceLine/TaxTotal/TaxSubtotal/TaxCategory/Percent",
"/InvoiceLine/TaxTotal/TaxSubtotal/TaxCategory/TaxScheme/ID",
"/InvoiceLine/TaxTotal/TaxSubtotal/TaxCategory/TaxScheme/Name"])
for tax_total in tree.xpath("//cac:TaxTotal", namespaces=ns):
csvwriter.writerow([get_value(tax_total, "/i2:Invoice/cbc:ID", ns),
get_value(tax_total, "../cbc:ID", ns),
get_value(tax_total, "../cbc:InvoicedQuantity", ns),
get_value(tax_total, "../cbc:LineExtensionAmount", ns),
get_value(tax_total, "cbc:TaxAmount", ns),
get_value(tax_total, "cac:TaxSubtotal/cbc:TaxableAmount", ns),
get_value(tax_total, "cac:TaxSubtotal/cbc:TaxAmount", ns),
get_value(tax_total, "cac:TaxSubtotal/cac:TaxCategory/cbc:ID", ns),
get_value(tax_total, "cac:TaxSubtotal/cac:TaxCategory/cbc:Percent", ns),
get_value(tax_total, "cac:TaxSubtotal/cac:TaxCategory/cac:TaxScheme/cbc:ID", ns),
get_value(tax_total, "cac:TaxSubtotal/cac:TaxCategory/cac:TaxScheme/cbc:Name", ns)])
输出
(输出.csv)
ID;/InvoiceLine/ID;/InvoiceLine/InvoicedQuantity;/InvoiceLine/LineExtensionAmount;/InvoiceLine/TaxTotal/TaxAmount;/InvoiceLine/TaxTotal/TaxSubtotal/TaxableAmount;/InvoiceLine/TaxTotal/TaxSubtotal/TaxAmount;/InvoiceLine/TaxTotal/TaxSubtotal/TaxCategory/ID;/InvoiceLine/TaxTotal/TaxSubtotal/TaxCategory/Percent;/InvoiceLine/TaxTotal/TaxSubtotal/TaxCategory/TaxScheme/ID;/InvoiceLine/TaxTotal/TaxSubtotal/TaxCategory/TaxScheme/Name
102165444;1.0000;1.0000;142.3900;138.24;142.39;7.20;3645;;140;Afgift
102165444;2.0000;1.0000;142.3900;138.24;142.39;7.20;3645;;140;Afgift
102165444;2.0000;1.0000;142.3900;35.60;142.39;35.60;StandardRated;25;63;Moms