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 + ' ') if __name__ == '__main__': word = r'C:UsersAdministrator.SC-201812211013PycharmProjects词表工作代码yiwoqucode症状替代文本.txt' word_replace(word)