python 高效去重复 支持GB级别大文件的示例代码

yipeiwu_com6年前Python基础

如下所示:

#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级别大文件的示例代码就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中的for循环

python中的for循环

Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。 语法: for循环的语法格式如下: for iterating_var in sequence:...

使用Python实现微信提醒备忘录功能

使用Python实现微信提醒备忘录功能

最近工作比较繁杂,经常忘事,有时候记了备忘录结果却忘记看备忘录,但是微信是每天都会看的,于是就想到写 一个基于微信的提醒系统。总体思路是将待办事项记录到在线记事本,通过建立定时任务,每天...

python使用 HTMLTestRunner.py生成测试报告

python使用 HTMLTestRunner.py生成测试报告

本文介绍了python使用 HTMLTestRunner.py生成测试报告 ,分享给大家,具体如下: HTMLTestRunner.py python 2版本 下载地址:http://t...

浅谈python中的变量默认是什么类型

1、type(变量名),输出的结果就是变量的类型; 例如 >>> type(6) <type 'int'> 2、在Python里面变量在声明时,不需要指定变...

mac安装pytorch及系统的numpy更新方法

安装Pytorch 在pytorch官网上选择相应选项,我的是OS X, pip, python2.7, none CUDA。 (之所以用python2.7只是觉得现在还有好多代码用2....