我正在使用to_xml编写xml,如下所示:-
df.to_xml(
os.path.join(file_path, f"{os.path.basename(file)}"),
index=False,
root_name="GenevaLoader",
namespaces=namespaces,
row_name="Expenses_New",
xml_declaration=False,
)
我明白了:
<GenevaLoader xmlns:GenevaLoader="blahh" xmlns:xsi="blahh" xmlns="" >
<Expenses_New>
<Comments>AAA</Comments>
<Portfolio>BBB</Portfolio>
<NetCounterAmount>1</NetCounterAmount>
</Expenses_New>
<Expenses_New>
<Comments>AAA</Comments>
<Portfolio>XXX</Portfolio>
<NetCounterAmount>2</NetCounterAmount>
</Expenses_New>
<Expenses_New>
<Comments>CCC</Comments>
<Portfolio>ZZZ</Portfolio>
<NetCounterAmount>3</NetCounterAmount>
</Expenses_New>
</GenevaLoader>
基本上,我想在所有的新标签周围添加一个TransactionRecord标签,这是我的行名,但下面是我的根名root\u name='GenevaLoader'
<GenevaLoader xmlns:GenevaLoader="blahh" xmlns:xsi="blahh" xmlns="">
<TransactionRecords>
<Expenses_New>
<Comments>AAA</Comments>
<Portfolio>BBB</Portfolio>
<NetCounterAmount>1</NetCounterAmount>
</Expenses_New>
<Expenses_New>
<Comments>AAA</Comments>
<Portfolio>XXX</Portfolio>
<NetCounterAmount>2</NetCounterAmount>
</Expenses_New>
<Expenses_New>
<Comments>CCC</Comments>
<Portfolio>ZZZ</Portfolio>
<NetCounterAmount>3</NetCounterAmount>
</Expenses_New>
</TransactionRecords>
</GenevaLoader>
正如@HenryEcker在评论中提到的,操作xml的最佳方法是使用xml解析器:
输出:
相关问题 更多 >
编程相关推荐