拆分大型 2GB XML 文件时出错 - UnicodeErrors: 'charmap' 编解码器...字符映射到<undefined>



我在Windows 10 64位上有2GB XML文件而没有成功。我在此处使用了GitHub上找到的一些代码,并设法将其进行操作,但是在特定字符 u0126上获得了UnicodeErrors(Maltese Alphabet中使用的字母)。脚本执行,但在保存第一个块并开始第二块之后,出现了错误。

编辑: XML文件是本地门户的DISQUS转储。

我遵循了此问题中找到的建议,并在Windows命令提示符和echo命令检查中设置chcp 65001setx PYTHONIOENCODING utf-8

我尝试了"可能已经有了您的答案的问题"中发现的许多解决方案,但我仍然在同一封信中获得UnicodeError。我还尝试了data.replace('Ħ', 'H')data.replace('\u1026', 'H')的原油,但是错误仍然出现并且处于相同的位置。每次我测试新事物大约需要5分钟,直到出现错误,并且我一直在为此而努力。

我尝试在记事本 64位中读取文件,但是当我搜索16GB RAM时,该程序最终没有做出响应,并且系统变得迟钝。

我不得不更改整个代码第一行的以下部分才能阅读:

cur_file = open(os.path.join(out_dir, root + FMT % cur_idx + ext), 'wt', encoding='utf-8')

以及第二行阅读:

with open(filename, 'rt', encoding='utf-8') as xml_file:

,但仍然没有果汁。我还使用了errors='replace'errors='ignore',但无济于事。

cur_file = open(os.path.join(out_dir, root + FMT % cur_idx + ext), 'wt')
with open(filename, 'rt') as xml_file:
    while True:
        # Read a chunk
        chunk = xml_file.read(CHUNK_SIZE)
        if len(chunk) < CHUNK_SIZE:
            # End of file
            # tell the parser we're done
            p.Parse(chunk, 1)
            # exit the loop
            break
        # process the chunk
        p.Parse(chunk)
# Don't forget to close our handle
cur_file.close()

我必须从原始代码编辑的另一行是:cur_file.write(data.encode('utf-8')),必须将其更改为:

cur_file.write(data)  # .encode('utf-8')) #*

否则执行使用TypeError: write() argument must be str, not bytes

停止
def char_data(data):
""" Called by the parser when he meet data """
global cur_size, start
wroteStart = False
if start is not None:
    # The data belong to an element, we should write the start part first
    cur_file.write('<%s%s>' % (start[0], attrs_s(start[1])))
    start = None
    wroteStart = True
# ``escape`` is too much for us, only & and < ned to be escaped there ...
data = data.replace('&', '&amp;')
data = data.replace('<', '&lt;')
if data == '>':
    data = '&gt;'
cur_file.write(data.encode('utf-8')) #*
cur_size += len(data)
if not wroteStart:
    # The data was outside of an element, it could be the right moment to
    # make the split
    next_file()

任何帮助将不胜感激。

编辑:添加了追溯问题总是在尝试编写文件时。

Traceback (most recent call last):
File "D:/Users/myself/ProjectForTesting/xml_split.py", line 249, in <module>
main(args[0], options.output_dir)
File "D:/Users/myself/ProjectForTesting/xml_split.py", line 229, in main
p.Parse(chunk)
File "..Modulespyexpat.c", line 282, in CharacterData
File "D:/Users/myself/ProjectForTesting/xml_split.py", line 180, in char_data
cur_file.write(data)  # .encode('utf-8'))
File "C:UsersmyselfAnaconda3libencodingscp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character 'u200e' in position 6: character maps to <undefined>

编辑:我尝试替换记事本 中的有问题的字符,但另一个' u200e'出现了,因此更换字符根本不强大。

我一直是一个菜鸟。我将写作修改为文件命令,以使用try: except块,该块仅将任何不需要的字符更改为空字符串。我知道该文件会失去一些这样的信息,但至少我可以将其拆分并在里面看!

这就是我所做的:

try:
cur_file.write(data)  # .encode('utf-8')) # this was part of the original line
except UnicodeEncodeError:
    data = ''
    cur_file.write(data)

最新更新