python - 是否有可能将熊猫数据框的每一行转换为预定义的文本文件?
问题描述
我的数据框如下所示:
我希望将每一行插入到预定义的文本文件中,以便这些值在文档中具有特定的位置。这就是我想出的:
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>
每行等等。有什么可能的方法来做到这一点?有人能帮我吗?
解决方案
你快到了。您可以使用字符串格式将值插入字符串中,如下所示:
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
推荐阅读
- powershell - Robocopy:复制文件,如果目标中存在相同的文件名,则保留两者
- sql - 使用 SQL 确保我有特定时间段的每一天的数据
- javascript - 如何从本地存储中获取项目?
- google-apps-script - 仅在之前未记录数据的情况下将发生的更改记录到单独的日志表中
- javascript - 是否存在由工作线程填充的阻塞 ArrayBuffer?
- laravel - 带有中间表的 Laravel 工厂
- excel - 使用组合框 VBA 中的选项抓取网站数据
- javascript - JavaScript 和 PHP 中的双向服务器/客户端通信
- bash - 使用 kubectl 通过 bash 脚本在 postgres 中插入 json
- r - 对数据进行子集化并创建一个显示最佳分布的图表