我寫了一篇文章,想用python來計算文章中的單詞。我將這篇文章粘貼在一個python文本文件中並保存。我寫了一個計劃,通過文本文件進行迭代和算的話,但它不斷給我下面的錯誤:"UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 62: character maps to <undefined>"
閱讀文件的Python問題
這是代碼:
def counter(file):
with open(file) as word:
count=0
for i in word:
words=i.split()
count+=words
print(count)
文件名essay.txt
它不會工作。即使當我嘗試在shell上打開essay.txt時,它也不起作用。 我試過以下內容:
infile = open('essay.txt')
word=infile.read()
print(word)
這也行不通。我該怎麼辦?請幫忙。謝謝
您是否檢查文件的位置62? – Theresa