Python去除文件中的重复行

倖福魔咒の 提交于 2020-01-06 14:55:22

Python去除文件中的重复行

PS: 也是从网上各个帖子中学习的Python,因此代码的格式以及内容有粘贴网上其他大神的代码,如有侵权请告知删除

界面示例
在这里插入图片描述

#!/usr/bin/python
# -*- coding: UTF-8 -*-

from tkinter import *
import tkinter.filedialog

root = Tk()
root.title('去除文件重复行')
root.geometry('300x200')
FileName1 = ""
FileName2 = ""

def Button1Anwser():
    global FileName1
    Path = Label(root, text='')
    FileName1 = tkinter.filedialog.askopenfilename()
    Path.config(text=FileName1)
    Path.grid(row=1, column=0, sticky=W)

def Button2Anwser():
    global FileName2
    Path = Label(root, text='')
    FileName2 = tkinter.filedialog.askopenfilename()
    Path.config(text=FileName2)
    Path.grid(row=3, column=0, sticky=W)

def Start():
    fp = open(FileName1, 'r+')
    fp0 = open(FileName2, 'w')
    Read = fp.read()
    Content = Read.split('\n')
    New_Content = []

    Text = Label(root, text='')
    Text.config(text='正在处理')
    Text.grid(row=4, column=1)

    for i in range(len(Content)):
        if Content[i] not in New_Content:
            New_Content.append(Content[i])
    for i in range(len(New_Content)):
        fp0.write(str(New_Content[i] + '\n'))
    fp0.close()

    Text = Label(root, text='')
    Text.config(text='处理完成')
    Text.grid(row=4, column=1)

# -------------------------------------界面---------------------------------
SelectFile1 = Button(root, text="源文件", command=Button1Anwser,width=10, height=1, bg="DarkGray", fg="white")
SelectFile1.grid(row=0, column=0, sticky=W)

SelectFile2 = Button(root, text="目标文件", command=Button2Anwser,width=10, height=1, bg="DarkGray", fg="white")
SelectFile2.grid(row=2, column=0, sticky=W)

ToDeal = Button(root, text="开始处理", command=Start,width=10, height=1, bg="DarkGray", fg="white")
ToDeal.grid(row=4, sticky=W)

root.mainloop()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!