Python 如何在使用lxml iterparse解析1GB XML文件时执行结束事件检查
我试图解析一个大约1GB的非常大的XML文件,其格式如下:Python 如何在使用lxml iterparse解析1GB XML文件时执行结束事件检查,python,xml,lxml,python-3.6,Python,Xml,Lxml,Python 3.6,我试图解析一个大约1GB的非常大的XML文件,其格式如下: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE candidates SYSTEM "dtd/mwetoolkit-candidates.dtd"> <!-- MWETOOLKIT: filetype="XML" --> <candidates> <meta> <corpussize name="ukwac-01"
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE candidates SYSTEM "dtd/mwetoolkit-candidates.dtd">
<!-- MWETOOLKIT: filetype="XML" -->
<candidates>
<meta>
<corpussize name="ukwac-01" value="38224449" />
<corpussize name="sum" value="38224449" />
</meta>
<cand candid="2">
<ngram><w lemma="executive" pos="JJ" ><freq name="ukwac-01" value="600" /><freq name="sum" value="600" /></w> <w lemma="box" pos="NNS" ><freq name="ukwac-01" value="1006" /><freq name="sum" value="1006" /></w> <freq name="ukwac-01" value="9" /><freq name="sum" value="9" /></ngram>
<occurs>
<ngram><w surface="Executive" lemma="executive" pos="JJ" /> <w surface="boxes" lemma="box" pos="NNS" /> <freq name="ukwac-01" value="1" /></ngram>
<ngram><w surface="executive" lemma="executive" pos="JJ" /> <w surface="boxes" lemma="box" pos="NNS" /> <freq name="ukwac-01" value="8" /></ngram>
</occurs>
</cand>
<cand candid="5">
<ngram><w lemma="bad" pos="JJ" ><freq name="ukwac-01" value="4094" /><freq name="sum" value="4094" /></w> <w lemma="thing" pos="NN" ><freq name="ukwac-01" value="6609" /><freq name="sum" value="6609" /></w> <freq name="ukwac-01" value="119" /><freq name="sum" value="119" /></ngram>
<occurs>
<ngram><w surface="bad" lemma="bad" pos="JJ" /> <w surface="thing" lemma="thing" pos="NN" /> <freq name="ukwac-01" value="115" /></ngram>
<ngram><w surface="Bad" lemma="bad" pos="JJ" /> <w surface="thing" lemma="thing" pos="NN" /> <freq name="ukwac-01" value="4" /></ngram>
</occurs>
</cand>
</candidates>
所需输出为以下格式的CSV文件:
mwe mwe_freq compounds
executive box 9 executive:600,box:1006
确切的打印格式可能会(也将)改变,但由于某种原因,一旦我进入打印功能并通过element.tag检查,freq元素将为空,我打印的只是它们的地址。我知道我应该在某个地方放一个结束事件检查,根据iterparse的,但我试着在fast_iter中放一个,但那肯定不起作用
我的当前输出:
mwe mwe_freq compounds
<Element freq at 0x7f8735342c48>
<Element freq at 0x7f8735342c88>
executive box 0
0
<Element freq at 0x7f8735346708>
<Element freq at 0x7f87353467c8>
bad thing 0
0
mwe-mwe\u-freq化合物
行政信箱0
0
坏事0
0
非常感谢您的帮助。为了缩小我的问题范围,调试器显示“freq”元素没有填充。我还编辑了代码中引用不存在的“features”元素的一部分,该元素不是当前xml格式。请解释您的目标是什么,并非常清楚地显示您在此处显示的输入文件的预期输出。谢谢。谢谢你的意见,我做了一些修改,希望我的问题现在更清楚。
mwe mwe_freq compounds
<Element freq at 0x7f8735342c48>
<Element freq at 0x7f8735342c88>
executive box 0
0
<Element freq at 0x7f8735346708>
<Element freq at 0x7f87353467c8>
bad thing 0
0