[英]Remove all characters except ASCII printable and chinese characters in large text file
我有一个 2GB 的文本文件,我想清理这个文件,使其只包含 ASCII 可打印字符和中文字符(大约 10000 个字符)。
我尝试了下面的两个代码,但它们都非常慢。 如果有任何建议,不胜感激。
chi_char = open(chinese_file,'r',encoding='UTF-8').read()
include = set(string.printable+all_chi_char)
full_text = open(source_file,'r',encoding='UTF-8').read()
output_text = ''.join(ch for ch in full_text if ch in include)
chi_char = open(chinese_file,'r',encoding='UTF-8').read()
include = set(string.printable+all_chi_char)
full_text = open(source_file,'r',encoding='UTF-8').read()
output_text = ''
for ch in full_text:
if ch in include:
output_text += ch
首先,你真的确定这是正确的做法吗? 很多时候,我们看到人们试图用随机的想法来启发式地清理他们的数据,即如何去除多余的东西,而不是从源头解决问题。 是否有办法在流程早期删除您不想要的内容,或者至少向我们解释为什么您的数据包含您不希望它包含的内容?
您当前方法的问题在于,您无缘无故地将整个文本文件一次加载到内存中。 Python 可能无法一次在常驻内存中拥有所有 2GB(加上它自己的代码和运行时状态所需的任何内容),因此操作系统将内存区域换出到磁盘,只是再次将它们换回,重复。
您最终需要将整个结果文本保存在内存中吗? 如果不是,则一次只读写一行,然后将该内存用于下一行文本。
with open(chinese_file,'r',encoding='UTF-8') as all_chi_char:
include = set(string.printable+all_chi_char.read())
with open(source_file,'r',encoding='UTF-8') as inp, open(dest_file, 'w') as outp:
for line in inp:
out_line = []
for ch in line:
if ch in include:
out_line.append(ch)
outp.write(''.join(out_line))
这仍然可以通过使用string.maketrans()
而不是本地字符set
来改进,但我猜这已经解决了性能问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.