Python 是否可以将熊猫数据框的每一行转换为预定义的文本文件?
我的数据框看起来是这样的: 我希望将每一行插入到预定义的文本文件中,以便这些值在文档中有一个特定的位置。 这就是我想到的:Python 是否可以将熊猫数据框的每一行转换为预定义的文本文件?,python,pandas,row,Python,Pandas,Row,我的数据框看起来是这样的: 我希望将每一行插入到预定义的文本文件中,以便这些值在文档中有一个特定的位置。 这就是我想到的: for i in range(len(df)): with open("%s.xml" %index, "w") as f: f.write( """<?xml version="1.0"?> <Invoice xmlns="urn:oa
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>
我的理想输出是第一行:
我的理想输出是第二行:
等等,每行。
有什么可行的方法可以做到这一点?有人能帮我吗?你快到了。可以使用字符串格式在字符串中插入值,如下所示:
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}"
参考资料:
你就快到了。可以使用字符串格式在字符串中插入值,如下所示:
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}"
参考资料:
如果您迭代这些行,您将得到一个索引的元组,其中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>
如果您迭代这些行,您将得到一个索引的元组,其中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>
请不要发布代码、数据或回溯的图像。复制并粘贴为文本,然后将其格式化为代码选择它并键入ctrl-k@第二次世界大战我认为一些指导或提示会很好,因为我真的很笨。请确保您阅读并理解本页面的工作原理,以及我们如何帮助您发挥最佳功能@第二次世界大战的评论恰恰相反,它很有帮助,并试图通过向你展示如何改进你的问题以获得答案来帮助你。如果你发布一个较小的示例,你的问题将更容易处理。包含初始化数据框架的代码,其中包含一些行和列以及要修改的小xml文档。目前,我们无法测试解决方案,您的示例xml非常混乱,不容易找到应该添加信息的位置。我知道你想从代码中得到什么,但是让未来的读者更容易理解问题的要点。请不要发布代码、数据或回溯的图像。复制并粘贴为文本,然后将其格式化为代码选择它并键入ctrl-k@第二次世界大战我认为一些指导或提示会很好,因为我真的很笨。请确保您阅读并理解本页面的工作原理,以及我们如何帮助您发挥最佳功能@第二次世界大战的评论恰恰相反,它很有帮助,并试图通过向你展示如何改进你的问题以获得答案来帮助你。如果你发布一个较小的示例,你的问题将更容易处理。包含初始化数据框架的代码,其中包含一些行和列以及要修改的小xml文档。目前,我们无法测试解决方案,您的示例xml非常混乱,不容易找到应该添加信息的位置。我知道你想从代码中得到什么,但是让未来的读者更容易理解问题的要点。谢谢你的评论,这对我帮助很大。fac_series_to_xml为我提供了一个名称错误:未定义名称“fac_series_to_xml”。我该怎么办?看来我把那个名字搞错了。。。会修好的。谢谢你的评论,这对我帮助很大。fac_series_to_xml为我提供了一个名称错误:未定义名称“fac_series_to_xml”。我该怎么办?看来我把那个名字搞错了。。。我会修好的。
# 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))