我需要使用python从大文本文件(500MiB)中删除带有Unicode字符的url,空行和行。删除Python中的网址,空行和Unicode字符
这是我的文件:
https://removethis1.com
http://removethis2.com foobar1
http://removethis3.com foobar2
foobar3 http://removethis4.com
www.removethis5.com
foobar4 www.removethis6.com foobar5
foobar6 foobar7
foobar8 www.removethis7.com
正则表达式后,它应该是这样的:
foobar1
foobar2
foobar3
foobar4 foobar5
foobar6 foobar7
foobar8
的代码我想出是这样的:
file = open(file_path, encoding="utf8")
self.rawFile = file.read()
rep = re.compile(r"""
http[s]?://.*?\s
|www.*?\s
|(\n){2,}
""", re.X)
self.processedFile = rep.sub('', self.rawFile)
但产量是不正确的:
foobar3 foobar4 foobar5
foobar6 foobar7
foobar8 www.removethis7.com
我还需要删除所有包含至少一个非ASCII字符的行,但我无法为此任务想出正则表达式。
不要一次做这一切,一行一行地去做 –
@PadraicCunningham我试过了,但是速度很慢 – Federico
你想改变原始文件内容还是创建一个新文件? –