2017-05-04 35 views
0

我想打開.txt文件和分隔列表中的詞語爲單獨的字符串(例如,[「這」,「是」,「一個」,「示例」])。 出於某種原因,當我嘗試這個,字母分開,我收到那不是我原來的文本文檔中開始怪異的符號和字符。的Python .TXT內容列出

file = open(userFile, "r") 
userInput = file.read() 
file.close() 
print(userInput) 

Screen shot of what I get when I run my code.

我的.txt文件僅含話 '別的東西' 反覆幾次。

+0

這個文件看起來就像是保存爲UTF-16 - 你使用任何編輯器或許可以被配置爲使用UTF-8或ASCII純文本,這將極大地更兼容。 – jasonharper

回答

1
with open("input.txt", "r") as infile: 
    lines = infile.readlines() 
infile.close() 
words = [word.strip() for word in lines] 

這適用於許多不同的文件編碼。

+0

ASCII也應該工作。 –

+0

@ArtemisFowl這是一年前我知識哈哈我更新了它 – quantik