爲什麼在Python做一個調用這個函數的結果輸出:簡單的讀檔問題
['54044', '14108', '79294', '29649']
['']
['']
函數被調用以這種方式:
的功能print read_file(FILENAME)
代碼:
def read_file(filename):
"""
Reads in the file and returns a unsorted list of all the numbers in the file.
"""
file = open(filename)
# Why don't these 2 do the same thing?
print file.read().split('\r\n')
print file.read().split('\r\n')
numbers = file.read().split("\r\n")
file.close()
return numbers
作爲一個旁註,要得到我需要使用轉義序列\ r和\ n分割文件的數字。對於在文件中連續使用這兩個轉義序列的人有沒有任何推理,而不僅僅是\ n? (該文件具有需要以特定方式排列的1000個數字的列表)
那是究竟如何你的程序的外觀?因爲'read_file'下的縮進不正確。 – RPGillespie 2014-10-29 15:36:15