Pandas to_xml()设置xml前缀

wgx48brx  于 2022-12-09  发布在  其他
关注(0)|答案(1)|浏览(136)

我尝试使用www.example.com _xml()从Pandas Dataframe 写入xmlpd.to,并获得以下输出:
密码:

# Write pandas dataframe to custom xml format

namespaces = {
    'ns0': "urn:sca:com:edi:mappings:aust:b2be:inbounddeliverydate"
}

with open('Inb.xml', 'w') as myfile: 
  myfile.write(data.to_xml(index=False, 
                            root_name='MT_InboundDeliveryDate', 
                            row_name='Row', 
                            namespaces=namespaces, 
                            prefix='ns0'))

输出量:

<?xml version='1.0' encoding='utf-8'?>
<ns0:MT_InboundDeliveryDate xmlns:ns0="urn:sca:com:edi:mappings:aust:b2be:inbounddeliverydate">
  <ns0:Row>
    <ns0:InboundID>355555106537455</ns0:InboundID>
    <ns0:DocumentDate/>
    <ns0:LFDAT>19082022</ns0:LFDAT>
  </ns0:Row>
  <ns0:Row>
    <ns0:InboundID>35555552066774536</ns0:InboundID>
    <ns0:DocumentDate/>
    <ns0:LFDAT>03012023</ns0:LFDAT>
  </ns0:Row>
</ns0:MT_InboundDeliveryDate>

但是,我需要将前缀仅应用于root_name,而不是每行
所需输出:

<?xml version='1.0' encoding='utf-8'?>
<ns0:MT_InboundDeliveryDate xmlns:ns0="urn:sca:com:edi:mappings:aust:b2be:inbounddeliverydate">
  <Row>
    <InboundID>355555106537455</InboundID>
    <DocumentDate/>
    <LFDAT>19082022</LFDAT>
  </Row>
  <Row>
    <InboundID>35555552066774536</InboundID>
    <DocumentDate/>
    <LFDAT>03012023</LFDAT>
  </Row>
</ns0:MT_InboundDeliveryDate>

我想实现上述所需的输出,以自动化我的系统更新脚本。

ni65a41a

ni65a41a1#

解决方法很简单:

namespaces = {
    "ns0":"urn:sca:com:edi:mappings:aust:b2be:inbounddeliverydate"
}

with open('Inb.xml', 'w') as myfile: 
  myfile.write(data.to_xml(index=False,
                            row_name="Row",
                            root_name=QName(namespaces["ns0"],'MT_InboundDeliveryDate')))

相关问题