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

使用 Xpath 處理較大的 XML 文件的最佳方法是什么

What is the best way to use Xpath for processing larger XML files?(使用 Xpath 處理較大的 XML 文件的最佳方法是什么?)
本文介紹了使用 Xpath 處理較大的 XML 文件的最佳方法是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個要求,我必須使用大型 XML(4 GB 文件)來查找其中的值.基本上我必須編寫大約 30 個不同的 Xpath 并將值存儲在一個列表中.當我嘗試解析 XML 時,它會引發內存錯誤.我已經嘗試使用 lxml 和 ElementTree 來處理開始和結束事件,但處理時間太長還是不行,而且我的 Pycharm/Jupyter 筆記本會引發內存錯誤.

I have a requirement where I have to use an large XML (4 GB file) for finding values in it. Basically I have to write around 30 different Xpath and store the values in a list. When I try to parse an XML, it throws memory error. I have tried using lxml and ElementTree with start and end events, still no luck the processing time is too high and my Pycharm/Jupyter notebook throws me memory error.

有沒有更好的方法呢?盡管此實現不限于任何編程語言,但我更喜歡 Python,因為它是我的右手.提前致謝.

Is there a better way to do it? Even though this implementation is not restricted to any programming language, I prefer Python because its my right hand. Thanks in advance.

例如搜索:如果我想要類別正在烹飪的年份的值.然后我使用 ./bookstore[@category=cooking]/book/year.所以值為 2005

Eg of a search: If I want the value of year where category is cooking. Then I use ./bookstore[@category=cooking]/book/year. So the value is 2005

同樣,我必須根據我的業務需求找到我的標簽的值.在我的要求中,XML 并不像下面的示例那樣簡單.

Similarly I have to find the values of my tags based on my business requirements. In my requirement the XML is not simple as the below example.

<?xml version="1.0" encoding="UTF-8"?>

<bookstore>

<book category="cooking">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>

<book category="children">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

<book category="web">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>

</bookstore>

推薦答案

不知道這樣能不能滿足你的需求.先試試看.如果有問題,我們可以繼續溝通.

I don't know if this will meet your needs. Try it first. If there is a problem, we can continue to communicate.

from simplified_scrapy import SimplifiedDoc,req,utils
def getBook(lines,category,year):
  html = "".join(lines)
  book = SimplifiedDoc(html).book
  if book.category==category and book.year.text==year:
    return book.category,book.title.text,book.authors.text,book.year.text,book.price.text
lst = []
with open('bookstore.xml', 'r') as file: # bookstore.xml is your xml file path
  lines = []
  flag = False
  for line in file:
    if flag or line.find('<book ')>=0:
      flag = True
      lines.append(line)
    if line.find('</book>')>=0:
      b = getBook(lines,'web','2003')
      if b: 
        lst.append(b)
        break # If you only want the first one, add a break
      flag = False
      lines = []
print (lst)

結果:

[('web', 'XQuery Kick Start', ['James McGovern', 'Per Bothner', 'Kurt Cagle', 'James Linn', 'Vaidyanathan Nagarajan'], '2003', '49.99')]

這篇關于使用 Xpath 處理較大的 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 對象)
主站蜘蛛池模板: 精品小视频 | 久久久久久毛片 | 久久一区二区视频 | 日韩精品成人 | 青青草福利视频 | 欧美一区免费 | 日本不卡免费 | 在线观看的av网站 | 一级性视频 | 欧美又大又硬又粗bbbbb | 精品在线免费观看 | 免费一级a毛片 | 一区二区三区四区在线视频 | 日本精品视频在线 | 三级黄色网址 | 欧美成人精品激情在线观看 | 日韩免费在线观看视频 | 成人网av | 国产69精品久久久久久 | 国产伦精品一区二区三区照片 | 超碰av在线播放 | 日韩福利在线 | 青青草免费在线 | 日本国产欧美 | 在线观看黄色小说 | 青青草一区二区 | 婷婷一区二区三区 | 在线播放中文字幕 | 欧美性猛交xxxx黑人交 | 黄色小视频在线观看 | 免费色片| 亚洲人成免费 | 欧美日韩一区二区三区四区 | 欧美日韩免费看 | 97国产精品视频 | 国产专区在线播放 | 国产毛片在线 | 欧美不卡在线 | 99久久婷婷国产综合精品草原 | 黄色录像一级片 | 视频爱爱免费视频爱爱太爽 |