【发布时间】:2019-12-16 09:54:49
【问题描述】:
我有类似这样结构的文件
<review>
<unique_id>
6301968972:just_as_true_today!:gary_d._thomas_"brass_crass"
</unique_id>
<unique_id>
5139
</unique_id>
<asin>
6301968972
</asin>
<product_name>
Hucksters: Video: Clark Gable,Deborah Kerr,Sydney Greenstreet,Adolphe Menjou,Ava Gardner,Keenan Wynn,Edward Arnold,Aubrey Mather,Richard Gaines,Frank Albertson,Douglas Fowley,Clinton Sundberg,Gloria Holden,Connie Gilchrist,Kathryn Card,Lillian Bronson,Vera Marshe,Ralph Bunker,Virginia Dale,Jimmy Conlin,Jack Conway
</product_name>
<product_type>
video
</product_type>
<product_type>
video
</product_type>
<helpful>
1 of 1
</helpful>
<rating>
5.0
</rating>
<title>
Just as true today!
</title>
<date>
August 4, 2000
</date>
<reviewer>
Gary D. Thomas "Brass Crass"
</reviewer>
<reviewer_location>
Studio City, CaliforniA
</reviewer_location>
<review_text>
Picture this: Clark Gable and the whole passle of MGM stars and starlets! Spins a great story, and all this in 1947, could be today, except they dressed better. You will not forget the characterizations, especially Sydney Greenstreet, eat your heart out Eddie Murphy, just a marvy little film for all ages (God I love cliches!
</review_text>
</review>
我希望它解析并将其转换为 datadframe。有哪些简单的方法可以做到这一点?
我尝试过 XML 和 HTML 解析器,但没有成功。 该文件包含上述 sn-p 的重复, 如果那里有任何图书馆,请提及。
谢谢
【问题讨论】:
-
Python xml parser 对我来说很好用。但是,有重复的标签,例如
unique_id或product_type- 您打算如何处理这些标签? -
解析器给了我错误:
junk after document element: line x, column y。我只需要同时使用"unique_id"s 和product type,因为我不能跳过它们 -
从您发布的示例中,我无法重现此内容。下一个问题是:如果您有重复的条目,那么从中创建
pandasDataFrame将很困难。 -
也许您的问题是您的根目录中有多个
子句 - 这在 xml 中不起作用;您将需要将它们“封装”在一个通用标签下,例如以 开始您的 xml 文件并以 结束(“内容”在这里是任意的)。 -
很高兴为您提供帮助。如果您在使用中有任何问题,请告诉我
标签: python dataframe parsing tags