久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在不使用 DOM 方法的情況下迭代解析大型 XML 文件

Iteratively parse a large XML file without using the DOM approach(在不使用 DOM 方法的情況下迭代解析大型 XML 文件)
本文介紹了在不使用 DOM 方法的情況下迭代解析大型 XML 文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 xml 文件

I have an xml file

<temp>
  <email id="1" Body="abc"/>
  <email id="2" Body="fre"/>
  .
  .
  <email id="998349883487454359203" Body="hi"/>
</temp>

我想讀取每個電子郵件標簽的 xml 文件.也就是說,有一次我想讀取電子郵件 id=1..從中提取正文,讀取的電子郵件 id=2...并從中提取正文...等等

I want to read the xml file for each email tag. That is, at a time I want to read email id=1..extract body from it, the read email id=2...and extract body from it...and so on

我嘗試使用 DOM 模型進行 XML 解析,因為我的文件大小為 100 GB..該方法不起作用.然后我嘗試使用:

I tried to do this using DOM model for XML parsing, since my file size is 100 GB..the approach does not work. I then tried using:

  from xml.etree import ElementTree as ET
  tree=ET.parse('myfile.xml')
  root=ET.parse('myfile.xml').getroot()
  for i in root.findall('email/'):
              print i.get('Body')

現在,一旦我獲得了 root..我不明白為什么我的代碼無法解析.

Now once I get the root..I am not getting why is my code not been able to parse.

使用 iterparse 時的代碼拋出以下錯誤:

The code upon using iterparse is throwing the following error:

 "UnicodeEncodeError: 'ascii' codec can't encode character u'u20ac' in position 437: ordinal not in range(128)"

誰能幫忙

推薦答案

一個iterparse的例子:

An example for iterparse:

import cStringIO
from xml.etree.ElementTree import iterparse

fakefile = cStringIO.StringIO("""<temp>
  <email id="1" Body="abc"/>
  <email id="2" Body="fre"/>
  <email id="998349883487454359203" Body="hi"/>
</temp>
""")
for _, elem in iterparse(fakefile):
    if elem.tag == 'email':
        print elem.attrib['id'], elem.attrib['Body']
    elem.clear()

只需將 fakefile 替換為您的真實文件即可.另請閱讀 this 了解更多詳情.

Just replace fakefile with your real file. Also read this for further details.

這篇關于在不使用 DOM 方法的情況下迭代解析大型 XML 文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 精品视频一区二区三区在线观看 | 黄一区二区三区 | 免费观看一级毛片 | 麻豆成人在线视频 | 亚洲综合无码一区二区 | 激情五月婷婷 | 亚洲在线一区 | 亚洲一区二区三区四区五区中文 | 毛片免费观看视频 | 91精品国产91久久久久久 | 精品三区| 久久福利 | 99精品在线观看 | 羞羞视频一区二区 | 亚洲精品99 | 国产成人免费视频网站高清观看视频 | 一级二级三级黄色 | 成人免费av在线 | 狠狠久久久 | 91精品久久久久久久 | 成人性生交大片免费看中文带字幕 | 在线小视频 | 精品一区二区免费视频 | 久久性色 | 日韩精品视频在线观看一区二区三区 | 99精品视频一区二区三区 | 成人精品一区 | 国产精品区一区二区三区 | 欧美成人aaa级毛片在线视频 | 国产亚洲欧美另类一区二区三区 | 精品一区在线 | 91麻豆精品国产91久久久更新资源速度超快 | 日韩高清国产一区在线 | 日韩欧美亚洲 | 国产美女一区二区 | 欧美美女一区二区 | 欧美在线视频a | 国产在线一区二区 | 日韩欧美在线播放 | 精品毛片 | 日本a v在线播放 |