我需要阅读我为用户raw_input使用空列表编写的现有文件。不完全确定我阅读后如何对文件进行排序。排序后,我需要将其保存在不同的文件名下并打印。 这是我到目前为止有:排序文件?
Names=[]
while 1:
Input = raw_input("Enter a name or press 'v' to quit:")
if Input == "v":
break
Names.append(Input)
raw_input ('Press Enter to write each of the names to a file named NAMES.')
text_file= open ("NAMES.txt", "w")
text_file.writelines(Names)
text_file.close()
raw_input('Press Enter to Read file into a sorted list.')
text_file = open("NAMES.txt", "r")
names = text_file.readlines()
text_file.close()
^这是我需要进行排序,并根据不同的文件名和保存打印。卡住!
你知道如何排序吗?你知道如何写入文件吗? – 2011-04-03 09:41:00
[这是一个对Python中排序机制的体面概述](http://wiki.python.org/moin/HowTo/Sorting/) - 它可能太快太快了 - 但坚持简单的结束的频谱,你不会犯太多错误。 – sarnold 2011-04-03 09:46:08
您不对文件排序,您对放置文件数据的数据结构进行排序。编程需要精确的措词。你应该改变标题。 – eyquem 2011-04-03 10:22:55