我需要閱讀我爲用戶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