with open("name.txt",'r') as read_file: for name in read_file: list_name = (name.split(',')) quchong = list(set(list_name)) with open("server_name",'w') as write_file: for item in quchong: write_file.writelines(item + ' ')