python文本去重并排序

import codecs

def word_replace(word):
    """替换词表"""
    word_list = []
    """去重"""
    with codecs.open(word,'r','utf8') as f:
        for line in f:
            line = line.strip()
            if line not in word_list:
                word_list.append(line)
                print(line)
            else:
                continue
    word_list.sort(key=len, reverse=True)
    with codecs.open(word,'w','utf8') as f:
        for line in word_list:
            print(line)
            f.write(line + '\n')
if __name__ == '__main__':
    word = r'C:\Users\Administrator.SC-201812211013\PycharmProjects\词表工作代码\yiwoqu\code\症状替代文本.txt'
    word_replace(word)

 

转载于:https://www.cnblogs.com/yiwoqu/p/11542044.html

你可能感兴趣的:(python文本去重并排序)