我想讀的文本文件,並從所有行提取每一個字,使字符串列表如下圖所示:從文件轉換爲文本字符串列表在python
['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east',
'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick',
'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']
我寫了這個代碼:
fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
lst.append(line.split())
print lst
print lst.sort()
當我對它進行排序時,它最終只給出了一個無。 我得到這個意外的結果!
[['But', 'soft', 'what', 'light', 'through', 'yonder', 'window', 'breaks'],
['It', 'is', 'the', 'east', 'and', 'Juliet', 'is', 'the', 'sun'], ['Arise',
'fair', 'sun', 'and', 'kill', 'the', 'envious', 'moon'], ['Who', 'is',
'already', 'sick', 'and', 'pale', 'with', 'grief']]
None
我完全失去了。我做錯了什麼?
文本文件的格式是什麼? – pzp
它是一種平面的文字file.But柔和什麼光那邊窗子裏 那是東方朱麗葉就是太陽 起來美麗的太陽,殺死羨慕月亮 誰已經面色慘白悲傷 –