问题描述
所以我有一个 52M
xml文件,该文件由 115139
元素组成.
So I have a 52M
xml file, which consists of 115139
elements.
from lxml import etree
tree = etree.parse(file)
root = tree.getroot()
In [76]: len(root)
Out[76]: 115139
我具有此功能,可以遍历 root
中的元素,并将每个已解析的元素插入Pandas DataFrame中.
I have this function that iterates over the elements within root
and inserts each parsed element inside a Pandas DataFrame.
def fnc_parse_xml(file, columns):
start = datetime.datetime.now()
df = pd.DataFrame(columns=columns)
tree = etree.parse(file)
root = tree.getroot()
xmlns = './/{' + root.nsmap[None] + '}'
for loc,e in enumerate(root):
tot = []
for column in columns:
tot.append(e.find(xmlns + column).text)
df.at[loc,columns] = tot
end = datetime.datetime.now()
diff = end-start
return df,diff
此过程有效,但是需要很多时间.我有一个配备16GB RAM的i7.
This process works but it takes a lot of time. I have an i7 with 16GB of RAM.
In [75]: diff.total_seconds()/60
Out[75]: 36.41769186666667
In [77]: len(df)
Out[77]: 115139
我敢肯定,有一种更好的方法可以将 52M
xml文件解析为Pandas DataFrame.
I'm pretty sure there is a better way of parsing a 52M
xml file into a Pandas DataFrame.
这是xml文件的摘录...
This is an extract of the xml file ...
<findToFileResponse xmlns="xmlapi_1.0">
<equipment.MediaIndependentStats>
<rxOctets>0</rxOctets>
<txOctets>0</txOctets>
<inSpeed>10000000</inSpeed>
<outSpeed>10000000</outSpeed>
<time>1587080746395</time>
<seconds>931265</seconds>
<port>Port 3/1/6</port>
<ip>192.168.157.204</ip>
<name>RouterA</name>
</equipment.MediaIndependentStats>
<equipment.MediaIndependentStats>
<rxOctets>0</rxOctets>
<txOctets>0</txOctets>
<inSpeed>100000</inSpeed>
<outSpeed>100000</outSpeed>
<time>1587080739924</time>
<seconds>928831</seconds>
<port>Port 1/1/1</port>
<ip>192.168.154.63</ip>
<name>RouterB</name>
</equipment.MediaIndependentStats>
</findToFileResponse>
关于如何提高速度的任何想法?
any ideas on how to improve speed?
对于上述xml的摘录,函数 fnc_parse_xml(file,columns)
返回此DF....
For the extract of the above xml, the function fnc_parse_xml(file, columns)
returns this DF....
In [83]: df
Out[83]:
rxOctets txOctets inSpeed outSpeed time seconds port ip name
0 0 0 10000000 10000000 1587080746395 931265 Port 3/1/6 192.168.157.204 RouterA
1 0 0 100000 100000 1587080739924 928831 Port 1/1/1 192.168.154.63 RouterB
推荐答案
另一种方法是使用 iterparse ...
Another option instead of building a tree by parsing the entire XML file is to use iterparse...
import datetime
import pandas as pd
from lxml import etree
def fnc_parse_xml(file, columns):
start = datetime.datetime.now()
# Capture all rows in array.
rows = []
# Process all "equipment.MediaIndependentStats" elements.
for event, elem in etree.iterparse(file, tag="{xmlapi_1.0}equipment.MediaIndependentStats"):
# Each row is a new dict.
row = {}
# Process all chidren of "equipment.MediaIndependentStats".
for child in elem.iterchildren():
# Create an entry in the row dict using the local name (without namespace) of the element for
# the key and the text content as the value.
row[etree.QName(child.tag).localname] = child.text
# Append the row dict to the rows array.
rows.append(row)
# Create the DateFrame. This would probably be better in a try/catch to handle errors.
df = pd.DataFrame(rows, columns=columns)
# Calculate time difference.
end = datetime.datetime.now()
diff = end - start
return df, diff
print(fnc_parse_xml("input.xml",
["rxOctets", "txOctets", "inSpeed", "outSpeed", "time", "seconds", "port", "ip", "name"]))
在我的计算机上,此文件将在4秒钟内处理92.5MB的文件.
On my machine this processes a 92.5MB file in about 4 seconds.
这篇关于提高将具有元素和名称空间的XML解析为Pandas的速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!