Python: 如何将动态变量传递给XML?
Python: How to pass dynamic variables into XML?
我正在使用 XMLs 的 USPS API。我希望能够将变量传递到 XML 树中,而不用 child 重建它 child。在 Python 中如何操作?
def fixed_xml_body_as_string(dest_zip, origin_zip):
#pass zip codes into XML
return """
https://secure.shippingapis.com/shippingapi.dll?API=RateV4&XML=
<RateV4Request USERID='XXXXXXXXXXX'>
<Revision>2</Revision>
<Package ID="1ST">
<Service>PRIORITY</Service>
<ZipOrigination></ZipOrigination>
<ZipDestination></ZipDestination>
<Pounds>1</Pounds>
<Ounces>0</Ounces>
<Container></Container>
<Width></Width>
<Length></Length>
<Height></Height>
<Girth></Girth>
<Machinable>false</Machinable>
</Package>
</RateV4Request>
"""
我想将邮政编码动态传递到 XML。
只需使用如下所示的字符串格式(请注意,您的 'xml' 无效)
def fixed_xml_body_as_string(dest_zip, origin_zip):
# pass zip codes into XML
return f"""
https://secure.shippingapis.com/shippingapi.dll?API=RateV4&XML=
<RateV4Request USERID='XXXXXXXXXXX'>
<Revision>2</Revision>
<Package ID="1ST">
<Service>PRIORITY</Service>
<ZipOrigination>{origin_zip}</ZipOrigination>
<ZipDestination>{dest_zip}</ZipDestination>
<Pounds>1</Pounds>
<Ounces>0</Ounces>
<Container></Container>
<Width></Width>
<Length></Length>
<Height></Height>
<Girth></Girth>
<Machinable>false</Machinable>
</Package>
</RateV4Request>
"""
print(fixed_xml_body_as_string(23, 99))
输出
https://secure.shippingapis.com/shippingapi.dll?API=RateV4&XML=
<RateV4Request USERID='XXXXXXXXXXX'>
<Revision>2</Revision>
<Package ID="1ST">
<Service>PRIORITY</Service>
<ZipOrigination>99</ZipOrigination>
<ZipDestination>23</ZipDestination>
<Pounds>1</Pounds>
<Ounces>0</Ounces>
<Container></Container>
<Width></Width>
<Length></Length>
<Height></Height>
<Girth></Girth>
<Machinable>false</Machinable>
</Package>
</RateV4Request>
我正在使用 XMLs 的 USPS API。我希望能够将变量传递到 XML 树中,而不用 child 重建它 child。在 Python 中如何操作?
def fixed_xml_body_as_string(dest_zip, origin_zip):
#pass zip codes into XML
return """
https://secure.shippingapis.com/shippingapi.dll?API=RateV4&XML=
<RateV4Request USERID='XXXXXXXXXXX'>
<Revision>2</Revision>
<Package ID="1ST">
<Service>PRIORITY</Service>
<ZipOrigination></ZipOrigination>
<ZipDestination></ZipDestination>
<Pounds>1</Pounds>
<Ounces>0</Ounces>
<Container></Container>
<Width></Width>
<Length></Length>
<Height></Height>
<Girth></Girth>
<Machinable>false</Machinable>
</Package>
</RateV4Request>
"""
我想将邮政编码动态传递到 XML。
只需使用如下所示的字符串格式(请注意,您的 'xml' 无效)
def fixed_xml_body_as_string(dest_zip, origin_zip):
# pass zip codes into XML
return f"""
https://secure.shippingapis.com/shippingapi.dll?API=RateV4&XML=
<RateV4Request USERID='XXXXXXXXXXX'>
<Revision>2</Revision>
<Package ID="1ST">
<Service>PRIORITY</Service>
<ZipOrigination>{origin_zip}</ZipOrigination>
<ZipDestination>{dest_zip}</ZipDestination>
<Pounds>1</Pounds>
<Ounces>0</Ounces>
<Container></Container>
<Width></Width>
<Length></Length>
<Height></Height>
<Girth></Girth>
<Machinable>false</Machinable>
</Package>
</RateV4Request>
"""
print(fixed_xml_body_as_string(23, 99))
输出
https://secure.shippingapis.com/shippingapi.dll?API=RateV4&XML=
<RateV4Request USERID='XXXXXXXXXXX'>
<Revision>2</Revision>
<Package ID="1ST">
<Service>PRIORITY</Service>
<ZipOrigination>99</ZipOrigination>
<ZipDestination>23</ZipDestination>
<Pounds>1</Pounds>
<Ounces>0</Ounces>
<Container></Container>
<Width></Width>
<Length></Length>
<Height></Height>
<Girth></Girth>
<Machinable>false</Machinable>
</Package>
</RateV4Request>