您好,我正在运行这段python代码,以将多行模式减少为单例模式.然而,我正在对200,000多行的超大文件执行此操作.
以下是我当前的代码:
import sys
import re
with open('largefile.txt', 'r+') as file:
string = file.read()
string = re.sub(r"((?:^.*\n)+)(?=\1)", "", string, flags=re.MULTILINE)
file.seek(0)
file.write(string)
file.truncate()
问题是re.sube()在我的大文件上占用了很长时间(10M以上).有没有可能以任何方式加快这一进程?
输入文件示例:
hello
mister
hello
mister
goomba
bananas
goomba
bananas
chocolate
hello
mister
输出示例:
hello
mister
goomba
bananas
chocolate
hello
mister
这些图案也可以大于2条线.