2012-02-28 4 views
2

大きな.xmlファイルがいくつかあります。私はいくつかのことをするためにファイルを解析したい。PythonでXMLを解析する

私だけ引き出すたい:

  • XML-/TITLE1をし、(例えば)
  • XML-/TITLE2の一覧を表示し、/ B
  • XML-を一覧表示するには、それを保存するためにそれを保存ライブラリは/使用をインポートするのがベストだろうPythonの2.xのを使用してC
  • など、など

を一覧表示することをTITLE3して保存します。どのように私はこれを設定するのですか? 提案がありますか?例については

<PubmedArticle> 
    <MedlineCitation Owner="NLM" Status="MEDLINE"> 
     <PMID Version="1">8981971</PMID> 
     <Article PubModel="Print"> 
      <Journal> 
       <ISSN IssnType="Print">0002-9297</ISSN> 
       <JournalIssue CitedMedium="Print"> 
        <Volume>60</Volume> 
        <Issue>1</Issue> 
        <PubDate> 
         <Year>1997</Year> 
         <Month>Jan</Month> 
        </PubDate> 
       </JournalIssue> 
       <Title>American journal of human genetics</Title> 
       <ISOAbbreviation>Am. J. Hum. Genet.</ISOAbbreviation> 
      </Journal> 
      <ArticleTitle>mtDNA and Y chromosome-specific polymorphisms in modern Ojibwa: implications about the origin of their gene pool.</ArticleTitle> 
      <Pagination> 
       <MedlinePgn>241-4</MedlinePgn> 
      </Pagination> 
      <AuthorList CompleteYN="Y"> 
       <Author ValidYN="Y"> 
        <LastName>Scozzari</LastName> 
        <ForeName>R</ForeName> 
        <Initials>R</Initials> 
       </Author> 
      </AuthorList> 
     <MeshHeadingList> 
      <MeshHeading> 
       <DescriptorName MajorTopicYN="N">Alleles</DescriptorName> 
      </MeshHeading> 
      <MeshHeading> 
       <DescriptorName MajorTopicYN="Y">Y Chromosome</DescriptorName> 
      </MeshHeading> 
     </MeshHeadingList> 
     <OtherID Source="NLM">PMC1712541</OtherID> 
    </MedlineCitation> 
</PubmedArticle> 
+1

私はこのため 'xml.dom.minidom'を使用すると思い、それは、Pythonに付属しており、正常に動作します。 'lxml'は良いライブラリですが、インストールする必要があります。 – kindall

答えて

2

Beautiful soupを試してみてください。私はこのライブラリがとても便利であることを発見しました。ちょうど指摘したように、BeautifulStoneSoupは具体的にはXMLを解析するためのものです。

+0

具体的には、BeautifulStoneSoup – Nishant

+0

ありがとう、私の答えを更新しました。 – varunl

+0

ありがとう、私は私のルートとしてBeautifulSoupを選びます。私はB.S.ドキュメンテーションはlxmlのそれよりもはっきりと分かりました。 – oaxacamatt

5

lxmlモジュールを見てみてください。

タイトルを見つけるには、Xpathをlxmlとするか、lxmlのxmlオブジェクト構造を使用してtitle要素まで「インデックスする」ことができます。

1

lxmlを試してください。

短い抜粋

>>> from lxml import etree 
>>> xml = """<foo><bar/>baz!</foo>""" 
>>> doc = etree.fromstring(xml) 
>>> doc.xpath('//foo/text()') #xpath expr 
['baz!'] 
>>> 

あなたはxml file

s = StringIO(xml) 
doc = etree.parse(s) 

よりを持っている場合は、 xpath exprを取得するために Firebug addonを使用することができます。

0

ElementTreeはすばらしく、Pythonに付属しています。

2

私はあなたの質問が私に信じさせてくれる、自分のリストの各タイトルをなぜ望んでいるのかわかりません。

1つのリスト内のすべてのタイトルはどうですか?次の例では、あなたのサンプルXMLのトリミングされたバージョンを使用して、プラス私はlxml.etree.xpathを使用すると、あなたのための<Title/>'sのリストを作成することを示すために<Article/>を重複:

>>> import lxml.etree 

>>> xml_text = """<PubmedArticle> 
    <MedlineCitation Owner="NLM" Status="MEDLINE"> 
    <PMID Version="1">8981971</PMID> 
    <Article PubModel="Print"> 
     <Journal> 
     <ISSN IssnType="Print">0002-9297</ISSN> 
     <!-- <JournalIssue ... /> --> 
     <Title>American journal of human genetics</Title> 
     <ISOAbbreviation>Am. J. Hum. Genet.</ISOAbbreviation> 
     </Journal> 
     <ArticleTitle>mtDNA and Y chromosome-specific polymorphisms in modern Ojibwa: implications about the origin of their gene pool.</ArticleTitle> 
     <!--<Pagination> 
      ... 
      </MeshHeadingList>--> 
     <OtherID Source="NLM">PMC1712541</OtherID> 
    </Article> 
    <Article PubModel="Print"> 
     <Journal> 
     <ISSN IssnType="Print">9297-0002</ISSN> 
     <!-- <JournalIssue ... /> --> 
     <Title>American Journal of Pediatrics</Title> 
     <ISOAbbreviation>Am. J. Ped.</ISOAbbreviation> 
     </Journal> 
     <ArticleTitle>Healthy Foo, Healthy Bar</ArticleTitle> 
     <!--<Pagination> 
      ... 
      </MeshHeadingList>--> 
     <OtherID Source="NLM">PMC1712541</OtherID> 
    </Article> 
    </MedlineCitation> 
</PubmedArticle>""" 

のXPathはlxml.etree.xpathはPythonのリストに変換されたノードを返すために作られています

>>> xml_obj = lxml.etree.fromstring(xml_text) 
>>> for title_obj in xml_obj.xpath('//Article/Journal/Title'): 
     print title_obj.text 

American journal of human genetics 
American Journal of Pediatrics 

EDIT 1:今、私が欲しかった

Pythonのxml.etree.ElementTreeとノードオブジェクトのサードパーティのモジュールをインストールすることが不可能であるか魅力的でない場合に備えて、含まれているモジュールでこのソリューションを表示する。

>>> import xml.etree.ElementTree as ETree 
>>> element = ETree.fromstring(xml_text) 
>>> xml_obj = ETree.ElementTree(element) 
>>> for title_obj in xml_obj.findall('.//Article/Journal/Title'): 
    print title_obj.text 


American journal of human genetics 
American Journal of Pediatrics 

それは小さなだが、このXPathはlxml例でXPathと同一でない次のとおりです。先頭にピリオド(「」)があります。期間がなければ、私は(Pythonの2.7.2で)この警告を得た:

>>> xml_obj.findall('//Article/Journal/Title') 

Warning (from warnings module): 
    File "__main__", line 1 
FutureWarning: This search is broken in 1.3 and earlier, and will be fixed in a future version. If you rely on the current behaviour, change it to './/Article/Journal/Title' 
+0

私は最終的にすべての回答を見て回りました。努力をいただきありがとうございます! lxmlライブラリを問題なくインストールすることができましたが、ドキュメントを通ってhellava時間を過ごしました。私はちょうどその時、頭を包むことができなかった。私はBeautifulSoupのドキュメントが少し扱いやすいことを発見しました。 – oaxacamatt

関連する問題