Skip to content Skip to sidebar Skip to footer

Python Xml To Csv

Please read entire question before marking duplicate. I have a nested XML file which i Want to convert to a csv file. I have to write a python script for same. The XML file is: &l

Solution 1:

Your XML has default namespace defined here :

<ListOrdersResponsexmlns="https://mws.amazonservices.com/Orders/2013-09-01">

Note that descendant elements inherits ancestor default namespace implicitly, unless otherwise specified. You need to combine namespace + local name to form a fully qualified element name, for example :

ns = {'d': 'https://mws.amazonservices.com/Orders/2013-09-01'}     fornodein tree.findall('.//d:Order', ns):
    oid = node.attrib.get('SellerOrderId')
    if oid:
        print oid

According to the full XML file you linked to, SellerOrderId is child element of Order instead of attribute. In this case, you can simply use .//d:Order/d:SellerOrderId to get them and then print it's value, like so :

ns = {'d': 'https://mws.amazonservices.com/Orders/2013-09-01'}  fornodein tree.findall('.//d:Order/d:SellerOrderId', ns):
    print node.text

output :

171-1322776-9700344
171-4214129-7148305
402-8263846-7042737
402-7017923-9474716
402-9691237-2887553
171-4614227-7597903
403-6729903-2119563
402-2184564-2676353
171-4520392-2088330
402-7986969-8827533

Post a Comment for "Python Xml To Csv"