我的数据文件看起来是这样的:
我希望将每一行插入到预定义的文本文件中,以便这些值在文档中有一个特定的位置。这就是我想出来的:
for i in range(len(df)):
with open("%s.xml" %index, "w") as f:
f.write(
"""<?xml version="1.0"?>
<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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd">
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0:extended:urn:www.simplerinvoicing.org:si:si-ubl:ver1.1.x</cbc:CustomizationID>
<cbc:ProfileID>urn:www.cenbii.eu:profile:bii04:ver2.0</cbc:ProfileID>
<cbc:ID> """df[Factuurdatum[i]]" </cbc:ID>
<cbc:IssueDate> Totaal </cbc:IssueDate>
<cbc:DueDate> Factuurdatum[i] </cbc:DueDate>"
<cbc:InvoiceTypeCode listID="UNCL1001" listAgencyID="6">380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>EUR</cbc:DocumentCurrencyCode>
<cac:AccountingSupplierParty>
--我的理想输出是第一行:
<?xml version="1.0"?>
<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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd">
<cbc:UBLVersionID>2.1</cbc:UBLVersionID> <cbc:CustomizationID>urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0:extended:urn:www.simplerinvoicing.org:si:si-ubl:ver1.1.x</cbc:CustomizationID>
<cbc:ProfileID>urn:www.cenbii.eu:profile:bii04:ver2.0</cbc:ProfileID>
<cbc:ID> ""0606194584" </cbc:ID>
<cbc:IssueDate> 12.93 </cbc:IssueDate>
<cbc:DueDate> 2020-09-18 </cbc:DueDate>"
<cbc:InvoiceTypeCode listID="UNCL1001" listAgencyID="6">380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>EUR</cbc:DocumentCurrencyCode>
<cac:AccountingSupplierParty>
--我的理想输出是第二行:
<?xml version="1.0"?>
<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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd">
<cbc:UBLVersionID>2.1</cbc:UBLVersionID> <cbc:CustomizationID>urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0:extended:urn:www.simplerinvoicing.org:si:si-ubl:ver1.1.x</cbc:CustomizationID>
<cbc:ProfileID>urn:www.cenbii.eu:profile:bii04:ver2.0</cbc:ProfileID>
<cbc:ID> ""20200633369" </cbc:ID>
<cbc:IssueDate> 30.25 </cbc:IssueDate>
<cbc:DueDate> 2020-06-26 </cbc:DueDate>"
<cbc:InvoiceTypeCode listID="UNCL1001" listAgencyID="6">380</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>EUR</cbc:DocumentCurrencyCode>
<cac:AccountingSupplierParty>
以此类推每一行。怎样才能做到这一点呢?有人能帮我吗?
发布于 2020-12-08 20:24:45
如果迭代行,就会得到(index, series)
的元组,其中series
保存单个行的列值。可以将该系列扩展为一个str.format
调用,该调用包含要生成的xml模板。作为一个简单的例子
>>> df=pd.DataFrame([[1,2,3],[4,5,6]], columns=['A','B','C'])
>>> df
A B C
0 1 2 3
1 4 5 6
>>> template = "<xml>\n <a>{A}</a>\n <b>{B}</b>\n <c>{C}</c>\n</xml>"
>>> for row in df.iterrows():
... print(template.format(**row[1]))
...
<xml>
<a>1</a>
<b>2</b>
<c>3</c>
</xml>
<xml>
<a>4</a>
<b>5</b>
<c>6</c>
</xml>
在扩展这个示例时,我将想要的文档分解成锅炉板,用于封装周围的xml文档,并为唯一的信息设置一个{fac_details}
格式变量。我不知道这个数据的好名字,所以我把它叫做"fac“--你需要更多的描述性的东西。我试图使xml更加完整,并且没有处理您感兴趣的所有列。
注意: OP不提供完整的运行程序,因此这是未经测试的伪代码。
# xml document to be expanding with per row details
fac_doc_template = """<?xml version="1.0"?>
<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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd">
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0:extended:urn:www.simplerinvoicing.org:si:si-ubl:ver1.1.x</cbc:CustomizationID>
<cbc:ProfileID>urn:www.cenbii.eu:profile:bii04:ver2.0</cbc:ProfileID>
{fac_details}
</cbc:CustomizationID>
</Invoice>"""
# per row details
# todo: expand for all of the column values you want
fac_details_xml_template = """
<cbc:ID>{Factuurnumer}</cbc:ID>
<cbc:IssueDate>{Factuurdatum}</cbc:IssueDate>
"""
def series_to_fac_details_xml(s):
return fac_details_xml_template.format(**s)
for index, row in df.iterrows():
details = series_to_fac_details_xml(row)
with open(f"{index}.xml", "w") as f:
f.write(fac_doc_template.format(fac_details=details))
发布于 2020-12-08 19:41:39
你快到了。可以使用字符串格式在字符串中插入值,如下所示:
data = "some data i want to insert"
result = "This is what I want to say: {}".format(data)
# or
result = f"This is what I want to say: {data}"
参考文献:
https://docs.python.org/3/library/stdtypes.html?highlight=format#str.format
https://stackoverflow.com/questions/65205599
复制相似问题