gpt4 book ai didi

python - 在 Python 中解析包含 XML 条目的文本/csv 文件

转载 作者:行者123 更新时间:2023-12-01 06:03:26 25 4
gpt4 key购买 nike

我有一个包含 XML 条目的 csv 文件。假设每个 XML 条目都以 <entry> 开头并以 </entry> 结尾。我的文件中有数千个这样的条目。每个 XML 条目都由嵌套的 XML 元素组成。

我需要提取每个条目的一些元素并通过Python将它们保存到另一个文件中。以下是一个 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/157748462321012736""/>
<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>"

最佳答案

使用csv解析 csv 的模块以及类似 elementtree 的内容解析 xml 字段。

如果您的 xml 数据与 RSS 兼容,请查看 feedparser .

关于python - 在 Python 中解析包含 XML 条目的文本/csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9169710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com