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

Python 中的大型 XML 文件解析

Large XML File Parsing in Python(Python 中的大型 XML 文件解析)
本文介紹了Python 中的大型 XML 文件解析的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個大小為 4 GB 的 XML 文件.我想解析它并將其轉(zhuǎn)換為數(shù)據(jù)框來處理它.但由于文件太大,以下代碼無法將文件轉(zhuǎn)換為 Pandas 數(shù)據(jù)框.代碼只是不斷加載,不提供任何輸出.但是當(dāng)我將它用于較小尺寸的類似文件時,我得到了正確的輸出.

I have an XML file of size 4 GB. I want to parse it and convert it to a Data Frame to work on it. But because the file size is too large the following code is unable to convert the file to a Pandas Data Frame. The code just keeps loading and does not provide any output. But when I use it for a similar file of smaller size I obtain the correct output.

任何人都可以提出任何解決方案.也許是一個代碼可以加快從 XML 到 Data Frame 的轉(zhuǎn)換過程或?qū)?XML 文件拆分為更小的子集.

Can anyone suggest any solution to this. Maybe a code that speeds up the process of conversion from XML to Data Frame or splitting of the XML file into smaller sub sets.

任何建議我是否應(yīng)該在我的個人系統(tǒng)(2 GB RAM)上處理如此大的 XML 文件,或者我應(yīng)該使用 Google Colab.如果是 Google Colab,那么有什么方法可以更快地將如此大的文件上傳到驅(qū)動器,從而更快地上傳到 Colab?

Any suggestion whether I should work with such large XML files on my personal system (2 GB RAM) or I should use Google Colab. If Google Colab, then is there any way to upload such large files quicker to drive and thus to Colab?

以下是我使用的代碼:

import xml.etree.ElementTree as ET
tree = ET.parse("Badges.xml")
root = tree.getroot()

#Column names for DataFrame
columns = ['row Id',"UserId",'Name','Date','Class','TagBased']

#Creating DataFrame
df = pd.DataFrame(columns = columns)

#Converting XML Tree to a Pandas DataFrame

for node in root: 
    
    row_Id = node.attrib.get("Id")
    UserId = node.attrib.get("UserId")
    Name = node.attrib.get("Name")
    Date = node.attrib.get("Date")
    Class = node.attrib.get("Class")
    TagBased = node.attrib.get("TagBased")
    
    df = df.append(pd.Series([row_Id,UserId,Name,Date,Class,TagBased], index = columns), ignore_index = True)

以下是我的 XML 文件:

Following is my XML File:

<badges>
  <row Id="82946" UserId="3718" Name="Teacher" Date="2008-09-15T08:55:03.923" Class="3" TagBased="False" />
  <row Id="82947" UserId="994" Name="Teacher" Date="2008-09-15T08:55:03.957" Class="3" TagBased="False" />
  <row Id="82949" UserId="3893" Name="Teacher" Date="2008-09-15T08:55:03.957" Class="3" TagBased="False" />
  <row Id="82950" UserId="4591" Name="Teacher" Date="2008-09-15T08:55:03.957" Class="3" TagBased="False" />
  <row Id="82951" UserId="5196" Name="Teacher" Date="2008-09-15T08:55:03.957" Class="3" TagBased="False" />
  <row Id="82952" UserId="2635" Name="Teacher" Date="2008-09-15T08:55:03.957" Class="3" TagBased="False" />
  <row Id="82953" UserId="1113" Name="Teacher" Date="2008-09-15T08:55:03.957" Class="3" TagBased="False" />

推薦答案

考慮 iterparse 用于以增量方式構(gòu)建樹的快速流處理.在每次迭代中構(gòu)建一個字典列表,然后您可以將其傳遞到 pandas.DataFrame 構(gòu)造函數(shù) once 外部循環(huán).下面調(diào)整根子節(jié)點的重復(fù)節(jié)點名稱:

Consider iterparse for fast streaming processing that builds tree incrementally. In each iteration build a list of dictionaries that you can then pass into pandas.DataFrame constructor once outside loop. Adjust below to name of repeating nodes of root's children:

from xml.etree.ElementTree import iterparse
#from cElementTree import iterparse
import pandas as pd

file_path = r"/path/to/Input.xml"
dict_list = []

for _, elem in iterparse(file_path, events=("end",)):
    if elem.tag == "row":
        dict_list.append({'rowId': elem.attrib['Id'],
                          'UserId': elem.attrib['UserId'],
                          'Name': elem.attrib['Name'],
                          'Date': elem.attrib['Date'],
                          'Class': elem.attrib['Class'],
                          'TagBased': elem.attrib['TagBased']})

        # dict_list.append(elem.attrib)      # ALTERNATIVELY, PARSE ALL ATTRIBUTES

        elem.clear()

df = pd.DataFrame(dict_list)

這篇關(guān)于Python 中的大型 XML 文件解析的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現(xiàn)問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 一区二区三区视频在线观看 | 99re视频| 免费看黄色av | 双性呜呜宫交受不住了h | 欧美成人一级片 | 免费一级大片 | 日日夜夜天天 | 久热99 | 国产乡下妇女三片 | 日韩色黄大片 | 色多多在线观看 | 国产精品久久久久久99 | 亚洲精品一二三四 | 成人av一区二区三区在线观看 | aaa免费视频 | 精品一区二区免费视频 | 四虎影院最新网址 | 一本色道久久综合亚洲精品小说 | 亚洲自拍偷拍视频 | 久久久久久99精品久久久 | 欧美高清一区二区 | www.国产在线观看 | av一二三 | 亚洲黄色三级 | 97精品视频在线观看 | 成av人片一区二区三区久久 | 毛片aaa | 久久人人爽| 日本黄色视 | 欧美性猛交99久久久久99按摩 | 中文字幕在线免费看 | 少妇视频网站 | 福利在线播放 | 99热| 日韩精品视频免费播放 | 成人午夜在线 | 成人精品在线观看 | 欧美成人黄色 | 精品在线免费视频 | 自拍偷拍专区 | 日韩国产精品视频 |