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中的对象拷贝示例 python引用传递

何谓引用传递,我们来看一个C++交换两个数的函数: 复制代码 代码如下:void swap(int &a, int &b){    int temp;&nb...

Python 检查数组元素是否存在类似PHP isset()方法

PHP中有isset方法来检查数组元素是否存在,在Python中无对应函数。 Python的编程理念是“包容错误”而不是“严格检查”。举例如下: 复制代码 代码如下: Look befo...

python的slice notation的特殊用法详解

python的slice notation的特殊用法详解

如下所示: python的slice notation的特殊用法。 a = [0,1,2,3,4,5,6,7,8,9] b = a[i:j] 表示复制a[i]到a[j-1],以生成新的...

python 系统调用的实例详解

python 系统调用的实例详解             &nbs...

Python写的PHPMyAdmin暴力破解工具代码

PHPMyAdmin暴力破解,加上CVE-2012-2122 MySQL Authentication Bypass Vulnerability漏洞利用。 #!/usr/bin/en...