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

yipeiwu_com5年前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设计】。

相关文章

pytorch numpy list类型之间的相互转换实例

如下所示: import torch from torch.autograd import Variable import numpy as np ''' pytorch中Varia...

Python中获取对象信息的方法

当我们拿到一个对象的引用时,如何知道这个对象是什么类型、有哪些方法呢? 使用type() 首先,我们来判断对象类型,使用type()函数: 基本类型都可以用type()判断: >...

Python时区设置方法与pytz查询时区教程

时区的概念与转换 首先要知道时区之间的转换关系,其实这很简单:把当地时间减去当地时区,剩下的就是格林威治时间了。 例如北京时间的18:00就是18:00+08:00,相减以后就是10:0...

使用tensorflow实现线性svm

本文实例为大家分享了tensorflow实现线性svm的具体代码,供大家参考,具体内容如下 简单方法: import tensorflow as tf import numpy a...

python将MongoDB里的ObjectId转换为时间戳的方法

本文实例讲述了python将MongoDB里的ObjectId转换为时间戳的方法。分享给大家供大家参考。具体分析如下: MongoDB里的_id字段前四位是时间戳的16进制表示,通过Py...