私はXMLエントリを含むcsvファイルを持っています。各XMLエントリが<entry>
で始まり、</entry>
で終わるとします。私のファイルには何千ものエントリがあります。各XMLエントリは、ネストされたXML要素で構成されます。PythonでXMLエントリを含むtext/csvファイルを解析する
私は各エントリのいくつかの要素を抽出し、それらを別のファイルにPythonで保存する必要があります。ここには、1つのXMLエントリのサンプルがあります。各エントリの要素と要素を抽出したいとします。 Pythonでこれをどうやってやれるのか教えてください。私はPythonプログラミングの初心者です。
"<entry xmlns=""http://www.w3.org/2005/Atom"" xmlns:gnip=""http://www.gnip.com/schemas/2010"">
<id>tag:search.twitter.com,2005:157796632933576704</id>
<published>2012-01-13T12:10:23+00:00</published>
<updated>2012-01-13T12:10:23+00:00</updated>
<summary type=""html"">RT @sprice54: If you rearrange the words ""Debit card"" you can spell ""Bad Credit""</summary>
<link rel=""alternate"" type=""text/html"" href=""http://twitter.com/GCordivari/statuses/157796632933576704""/>
<source>
<link rel=""self"" type=""application/json"" href=""https://stream.twitter.com/1/statuses/filter.json""/>
<title>Twitter - Stream - Track</title>
<updated>2012-01-13T12:10:25Z</updated>
</source>
<service:provider xmlns:service=""http://activitystrea.ms/service-provider"">
<name>Twitter</name>
<uri>http://www.twitter.com/</uri>
<icon/>
</service:provider>
<contributor>
<name>Steve Price</name>
<uri>http://www.twitter.com/sprice54</uri>
</contributor>
<link rel=""via"" type=""text/html"" href=""http://twitter.com/sprice54/statuses/12736""/>
<title>George Cordivari shared: Steve Price posted a note on Twitter</title>
<category term=""StatusShared"" label=""Status Shared""/>
<category term=""NoteShared"" label=""Note Shared""/>
<activity:verb xmlns:activity=""http://activitystrea.ms/spec/1.0/"">http://activitystrea.ms/schema/1.0/share</activity:verb>
<activity:object xmlns:activity=""http://activitystrea.ms/spec/1.0/"">
<activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
<id>object:search.twitter.com,2005:157796632933576704</id>
<content type=""html"">RT @sprice54: If you rearrange the words ""Debit card"" you can spell ""Bad Credit""</content>
<link rel=""alternate"" type=""text/html"" href=""http://twitter.com/GCordivari/statuses/157796632933576704""/>
</activity:object>
<author>
<name>George Cordivari</name>
<uri>http://www.twitter.com/GCordivari</uri>
</author>
<activity:author xmlns:activity=""http://activitystrea.ms/spec/1.0/"">
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<gnip:friends xmlns:gnip=""http://www.gnip.com/schemas/2010"" followersCount=""37"" followingCount=""61""/>
<link rel=""alternate"" type=""text/html"" length=""0"" href=""http://www.twitter.com/GCordivari""/>
<link rel=""avatar"" href=""http://a0.twimg.com/profile_images/1670548060/274805_1268643462_1179159089_n_normal.jpg""/>
<id>http://www.twitter.com/GCordivari</id>
</activity:author>
<activity:actor xmlns:activity=""http://activitystrea.ms/spec/1.0/"">
<activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
<gnip:friends xmlns:gnip=""http://www.gnip.com/schemas/2010"" followersCount=""37"" followingCount=""61""/>
<gnip:stats xmlns:gnip=""http://www.gnip.com/schemas/2010"" activityCount=""370"" upstreamId=""id:twitter.com:427031045""/>
<link rel=""alternate"" type=""text/html"" length=""0"" href=""http://www.twitter.com/GCordivari""/>
<link rel=""avatar"" href=""http://a0.twimg.com/profile_images/1670548060/274805_1268643462_1179159089_n_normal.jpg""/>
<id>http://www.twitter.com/GCordivari</id>
<os:location xmlns:os=""http://ns.opensocial.org/2008/opensocial"">Drexel Hell</os:location>
<os:aboutMe xmlns:os=""http://ns.opensocial.org/2008/opensocial"">This is the way I live. #CirocInMyCupIDGAF #CloudNine #FollowMeLikeTheLeader </os:aboutMe>
</activity:actor>
<gnip:twitter_entities xmlns:gnip=""http://www.gnip.com/schemas/2010"">
<user_mentions>
<user_mention start=""3"" end=""12"">
<id>255347428</id>
<name>Steve Price</name>
<screen_name>sprice54</screen_name>
</user_mention>
</user_mentions>
</gnip:twitter_entities>
<gnip:matching_rules>
<gnip:matching_rule rel=""inferred"">""debit card""</gnip:matching_rule>
</gnip:matching_rules>
</entry>"
ありがとうございました。あなたのコードを実行すると、このエラーが発生します:parseStringの "/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/minidom.py"、行1924、 return expatbuilder.parseString string) ファイル "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/dom/expatbuilder.py"、940行目、parseString 戻り値builder.parseString(string) ファイル "/ parseStringの のparser.Parse(文字列、True) xml.parsers.expat.ExpatError:ライブラリ223 /ライブラリ/フレームワーク/ Python.framework /バージョン/ 2.7/lib/python2.7/xml/dom/expatbuilder.py "構文エラー – saghar
パーサが、提供された文字列を解析できなかったということです。私は、XMLの形式が間違っているとか、そうでなければ組み込みパーサーが理解できないものと思われます。あなたは他のパーサーと運が良いかもしれません。 XMLを制御できない場合は、何かを試してみることをお勧めします。 – tharen
データを「XMLエントリを含むcsvファイル」と記述しましたが、これは「[xmldata]、[xmldata]、...」を意味しました。 xmldataには ... が含まれています。これが間違っている場合は、より多くの文脈を提供する必要があります。 –
tharen