python - 使用 beautifulsoup 将大型 xml 文件拆分为多个文件

标签 python xml beautifulsoup

我正在尝试将大型 xml 文件拆分为较小的文件,首先我从 beautifulsoup 开始:

from bs4 import BeautifulSoup
import os
# Core settings
rootdir = r'C:\Users\XX\Documents\Grant Data\2010_xml'
extension = ".xml"
to_save = r'C:\Users\XX\Documents\all_patents_as_xml'

index = 0
for root, dirs, files in os.walk(rootdir):
    for file in files:
        if file.endswith(extension):
            print(file)
            file_name = os.path.join(root,file)
            with open(file_name) as f:
                data = f.read()
            texts = data.split('?xml version="1.0" encoding="UTF-8"?')
            for text in texts:
                index += 1
                filename = to_save + "\\"+ str(index) + ".txt"
                with open(filename, 'w') as f:
                    f.write(text)

但是,我遇到了内存错误。然后我切换到xml etree:

from xml.etree import ElementTree as ET
import re


file_name = r'C:\Users\XX\Documents\Grant Data\2010_xml\2010cat_xml.xml'


with open(file_name) as f:
    xml = f.read()
tree = ET.fromstring(re.sub(r"(<\?xml[^>]+\?>)", r"\1<root>", xml) + "</root>")
parser = ET.iterparse(tree)
to_save = r'C:\Users\Yilmaz\Documents\all_patents_as_xml'
index = 0
for event, element in parser:
    # element is a whole element
    if element.tag == '?xml version="1.0" encoding="UTF-8"?':
        index += 1
        filename = to_save + "\\"+ str(index) + ".txt"
        with open(filename, 'w') as f:
            f.write(ET.tostring(element))
        # do something with this element
        # then clean up
        element.clear()

我收到以下错误:

OverflowError: size does not fit in an int

我使用的是 Windows 操作系统,我知道在 Linux 中你可以从 consule 中拆分 xml,但就我而言,我不知道该怎么做。

最佳答案

如果您的 XML 由于内存限制而无法加载,您应该考虑使用 SAX

使用 SAX,您将阅读文档的“一小部分”,然后对它们执行您想要执行的操作(示例:将每 N 个元素保存到一个新文件中)。

Python SAX example 1

Python SAX example 2

关于python - 使用 beautifulsoup 将大型 xml 文件拆分为多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56461707/

相关文章:

c# - 向 XML 文件添加注释并使用 C# 读入

python - 从文件加载大型数组,numpy 比列表追加慢。瓶颈在哪里?

python :!=和<>之间的区别?

c# - StreamReader 和读取 XML 文件

python - 如何搜索匹配的字符串,然后提取其后面的字符串和冒号

python - 如何在 Python Beautiful Soup 中获取没有唯一元素的特定文本信息?

python - 使用 beautifulsoup 从页面中抓取表格,找不到表格

python - 如何使用 boto3 有条件地将项目插入 dynamodb 表

python - 多个字段到同一个数据库列

c# - 如何选择使用默认命名空间的节点?