2015-06-08 71 views
1

請在標記重複之前閱讀整個問題。
我有一個嵌套的XML文件,我想轉換爲CSV文件。 我必須爲它編寫一個python腳本。Python xml to csv

XML文件是:

<?xml version="1.0"?> 
<ListOrdersResponse xmlns="https://mws.amazonservices.com/Orders/2013-09-01"> 
    <ListOrdersResult> 
    <Orders> 
     <Order> 
     <LatestShipDate>2015-06-02T18:29:59Z</LatestShipDate> 
     <OrderType>StandardOrder</OrderType> 
     <PurchaseDate>2015-05-31T03:58:30Z</PurchaseDate> 
     <AmazonOrderId>171-6355256-9594715</AmazonOrderId> 
     <LastUpdateDate>2015-06-01T04:18:58Z</LastUpdateDate> 
     <ShipServiceLevel>IN Std Domestic</ShipServiceLevel> 
     <NumberOfItemsShipped>0</NumberOfItemsShipped> 
     <OrderStatus>Canceled</OrderStatus> 
     <SalesChannel>Amazon.in</SalesChannel> 
     <NumberOfItemsUnshipped>0</NumberOfItemsUnshipped> 
     <IsPremiumOrder>false</IsPremiumOrder> 
     <EarliestShipDate>2015-05-31T18:30:00Z</EarliestShipDate> 
     <MarketplaceId>A21TJRUUN4KGV</MarketplaceId> 
     <FulfillmentChannel>MFN</FulfillmentChannel> 
     <IsPrime>false</IsPrime> 
     <ShipmentServiceLevelCategory>Standard</ShipmentServiceLevelCategory> 
    </Order> 
    <Order> 
     <LatestShipDate>2015-06-02T18:29:59Z</LatestShipDate> 
     <OrderType>StandardOrder</OrderType> 
     <PurchaseDate>2015-05-31T04:50:07Z</PurchaseDate> 
     <BuyerEmail>[email protected]</BuyerEmail> 
     <AmazonOrderId>403-5551715-2566754</AmazonOrderId> 
     <LastUpdateDate>2015-06-01T07:52:49Z</LastUpdateDate> 
     <ShipServiceLevel>IN Exp Dom 2</ShipServiceLevel> 
     <NumberOfItemsShipped>2</NumberOfItemsShipped> 
     <OrderStatus>Shipped</OrderStatus> 
     <SalesChannel>Amazon.in</SalesChannel> 
     <ShippedByAmazonTFM>false</ShippedByAmazonTFM> 
     <LatestDeliveryDate>2015-06-06T18:29:59Z</LatestDeliveryDate> 
     <NumberOfItemsUnshipped>0</NumberOfItemsUnshipped> 
     <BuyerName>Ajit Nair</BuyerName> 
     <EarliestDeliveryDate>2015-06-02T18:30:00Z</EarliestDeliveryDate> 
     <OrderTotal> 
      <CurrencyCode>INR</CurrencyCode> 
      <Amount>938.00</Amount> 
     </OrderTotal> 
     <IsPremiumOrder>false</IsPremiumOrder> 
     <EarliestShipDate>2015-05-31T18:30:00Z</EarliestShipDate> 
     <MarketplaceId>A21TJRUUN4KGV</MarketplaceId> 
     <FulfillmentChannel>MFN</FulfillmentChannel> 
     <TFMShipmentStatus>Delivered</TFMShipmentStatus> 
     <PaymentMethod>Other</PaymentMethod> 
     <ShippingAddress> 
      <StateOrRegion>MAHARASHTRA</StateOrRegion> 
      <City>THANE</City> 
      <Phone>9769994355</Phone> 
      <CountryCode>IN</CountryCode> 
      <PostalCode>400709</PostalCode> 
      <Name>Ajit Nair</Name> 
      <AddressLine1>C-25/con-7/Chandralok CHS</AddressLine1> 
      <AddressLine2>Sector-10 ,Koper khairne</AddressLine2> 
     </ShippingAddress> 
     <IsPrime>false</IsPrime> 
     <ShipmentServiceLevelCategory>Expedited</ShipmentServiceLevelCategory> 
     </Order> 

我試圖讓值我以列表的形式代碼。但它不打印任何東西。

我的代碼:

from xml.etree import ElementTree 

with open('orders.xml', 'rb') as f: 
tree = ElementTree.parse(f) 

for node in tree.findall('.//Order'): 
    oid = node.attrib.get('SellerOrderId') 
    if oid: 
     print oid 

什麼是錯我的代碼?

編輯:臨時鏈接,即可完成文件Orders.xml

+0

哪裏是'SellerOrderId'在XML? – konart

+0

我沒有在任何地方看到'SellerOrderId'。你在期待什麼? –

+0

它只是一個部分... SellerOrderId存在於許多標籤中,但不是全部。我有一個鏈接來完成文件在問題底部的鏈接。 –

回答

4

你的XML有此定義默認命名空間

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

注意後代元素繼承祖先的默認命名空間隱含,除非另有說明。您需要結合空間+本地名,形成一個完全合格的元素名稱,例如:

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

根據您鏈接到完整的XML文件,SellerOrderIdOrder的屬性,而不是子元素。在這種情況下,你可以簡單地使用.//d:Order/d:SellerOrderId,讓他們,然後打印出來的價值,像這樣:

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

輸出:

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