Chinaunix首页 | 论坛 | 博客
  • 博客访问: 91583
  • 博文数量: 30
  • 博客积分: 1235
  • 博客等级: 中尉
  • 技术积分: 295
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-20 21:15
文章分类

全部博文(30)

文章存档

2010年(8)

2009年(22)

我的朋友

分类: Python/Ruby

2009-11-12 14:12:09

去除文件中的重复行,前提:文件已经经过排序


#!/usr/bin/env python

#去除重复行

import sys
import os

def printUsage():
    print("Usage: python uniq.py sourcefile ")

def printFileNotExist():
    print(":( Sorry, File " , sys.argv[1], " does not exist!")

def uniqFile(filename):
    fp = open(filename,"r")
    preline = '';
    for line in fp:
        line.strip()
        if line == preline:
            continue
        print(line,end="")
        preline = line    
    
if __name__ == "__main__":
    if len(sys.argv) != 2:
        printUsage()
        exit()

    if not os.path.isfile(sys.argv[1]):
        printFileNotExist()
        exit()

    uniqFile(sys.argv[1])






阅读(1053) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~