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中zip()函数用法实例教程

本文实例讲述了Python中zip()函数的定义及用法,相信对于Python初学者有一定的借鉴价值。详情如下: 一、定义: zip([iterable, ...]) zip()是Pyth...

Python创建xml的方法

本文实例讲述了Python创建xml的方法。分享给大家供大家参考。具体实现方法如下: from xml.dom.minidom import Document class write...

python统计一个文本中重复行数的方法

本文实例讲述了python统计一个文本中重复行数的方法。分享给大家供大家参考。具体实现方法如下: 比如有下面一个文件 2 3 1 2 我们期望得到 2,2 3,1 1,1 解决问题的思路...

Python在不同目录下导入模块的实现方法

python在不同层级目录import模块的方法 使用python进行程序编写时,经常会调用不同目录下的模块及函数。本篇博客针对常见的模块调用讲解导入模块的方法。 1. 同级目录下的调用...

如何用python写一个简单的词法分析器

如何用python写一个简单的词法分析器

编译原理老师要求写一个java的词法分析器,想了想决定用python写一个。 目标 能识别出变量,数字,运算符,界符和关键字,用excel表打印出来。 有了目标,想想要怎么实现词法分析器...