2013-04-06 103 views
1

我有一個具有巨大數據大小的XML文件。使用SAX將XML轉換爲CSV Python

XML文件包含

<us-bibliographic-data-grant> 
    <publication-reference> 
     <document-id> 
      <country>US</country> 
      <doc-number>D0607176</doc-number> 
      <kind>S1</kind> 
      <date>20100105</date> 
     </document-id> 
    </publication-reference> 

    <application-reference appl-type="design"> 
     <document-id> 
      <country>US</country> 
      <doc-number>29327507</doc-number> 
      <date>20081107</date> 
     </document-id> 
    </application-reference> 

    <invention-title id="d0e55">Doughnut product with six appendages</invention-title> 

    <applicants> 
     <applicant sequence="001" app-type="applicant-inventor" designation="us-only"> 
      <addressbook> 
      <last-name>Peters</last-name> 
      <first-name>Brian Jeffery</first-name> 
       <address> 
        <street>7052 Moonlight Cir.</street> 
        <city>Huntington Beach</city> 
        <state>CA</state> 
        <postcode>92647</postcode> 
        <country>US</country> 
       </address> 
      </addressbook> 
      <nationality> 
       <country>omitted</country> 
      </nationality> 
      <residence> 
       <country>US</country> 
      </residence> 
     </applicant> 
    </applicants> 
</us-bibliographic-data-grant> 

我怎樣才能得到這樣

last-name,first-name,street,city,state,postcode,country 
peters,brian jeffery, 7052 moonlight cir.,huntington beach,CA,92647 

我沒有任何經驗可言的XML輸出,請幫忙 在這個XML代碼中,有很多<addressbook>。那麼如何獲得第一行中的applicant內的所有屬性,並且下一行由所有值填充。我想這樣做,因爲我想稍後將csv文件導入到sql。

+0

此xml無效。 – dawg 2013-04-06 18:01:15

+0

爲什麼?我只是複製了它的一部分,很多有一些startElements沒有結束元素 – 2013-04-07 01:53:34

+0

粘貼XML [HERE](http://www.xmlvalidation.com)或其他一些驗證器。嘗試通過解析器運行它。它無效或完全形成。 – dawg 2013-04-07 05:32:51

回答

0

如果你只是想從一種文件類型轉換到另一種文件類型,你應該使用一個工具。這裏有一個建議 - https://code.google.com/p/xml2csv-conv/

我在過去使用過xslt,當時我有複雜的xml文檔來解釋。

+0

我被要求使用Python進行此操作。只轉換地址部分...請幫助 – 2013-04-07 02:21:24