2013-04-06 120 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