XML 到 Pandas 数据框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10947968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XML to pandas dataframe
提问by root
I have a XML file with thousands of lines like:
我有一个包含数千行的 XML 文件,例如:
<Word x1="206" y1="120" x2="214" y2="144" font="Times-Roman" style="font-size:22pt">WORD</Word>
I want to convert it (all it's attributes) to pandasdataframe. To do that i could loop through the file using beautiful soup and insert the values row by row or create lists to be inserted as columns. However I would like to know if there's a more pythonic way of accomplishing what I described. Thank you in advance.
我想将它(所有它的属性)转换为pandasdataframe. 为此,我可以使用漂亮的汤遍历文件并逐行插入值或创建要作为列插入的列表。但是,我想知道是否有更pythonic 的方式来完成我所描述的。先感谢您。
Code example:
代码示例:
x1list=[]
x2list=[]
for word in soup.page.findAll('word'):
x1list.append(int(word['x1']))
x2list.append(int(word['x2']))
df=DataFrame({'x1':x1list,'x2':x2list})
采纳答案by eumiro
Try this:
尝试这个:
DataFrame.from_records([(int(word['x1']), int(word['x2']))
for word in soup.page.findAll('word')],
columns=('x1', 'x2'))

