码迷,mamicode.com
首页 > 其他好文 > 详细

删除重复行

时间:2017-08-30 13:12:15      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:error   list   usr   style   main   with   env   log   lis   

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: f0rsaken

import sys

def main():
    try:
        filename = sys.argv[1]
    except IndexError as e:
        print(e)
        sys.exit(2)

    try:
        with open(filename, "r") as f:
            temp_list = f.readlines()
    except IOError as e:
        print(e)
        sys.exit(1)

    content_list = list()
    for temp in temp_list:
        content_list.append(temp.rstrip() + "\n")

    new_content_list = list()
    for content in content_list:
        if content not in new_content_list:
            new_content_list.append(content)

    with open("new_" + filename, "w") as f:
        f.writelines(new_content_list)

if __name__ == "__main__":
    main()

 

删除重复行

标签:error   list   usr   style   main   with   env   log   lis   

原文地址:http://www.cnblogs.com/forsaken/p/7452247.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!