Python实现GB级大文件高效去重脚本
本文分享一个基于Python的大文件去重示例代码,支持GB级别数据。通过正则表达式提取词元并利用集合去重,实现高效处理。代码包含文件读取、去重逻辑及结果保存功能,适用于需要处理海量文本数据的场景。
具体实现代码如下所示:
#coding=utf-8
import sys, re, os
def getDictList(dict):
regx = '''[\w\~`\!\@\#\$\%\^\&\*\(\)\_\-\+\=\[\]\{\}\:\;\,\.\/\<\>\?]+'''
with open(dict) as f:
data = f.read()
return re.findall(regx, data)
def rmdp(dictList):
return list(set(dictList))
def fileSave(dictRmdp, out):
with open(out, 'a') as f:
for line in dictRmdp:
f.write(line + '\n')
def main():
try:
dict = sys.argv[1].strip()
out = sys.argv[2].strip()
except Exception, e:
print 'error:', e
me = os.path.basename(__file__)
print 'usage: %s <input> <output>' %me
print 'example: %s dict.txt dict_rmdp.txt' %me
exit()
dictList = getDictList(dict)
dictRmdp = rmdp(dictList)
fileSave(dictRmdp, out)
if __name__ == '__main__':
main()
上述关于Python高效去重且支持GB级别大文件的示例代码即为完整分享。希望此内容能为您提供参考,也欢迎继续支持本站获取更多技术资源。
免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。









