在學習機器學習實戰這本書的時候,編譯書本上的程式時出現了很多的錯誤,現總結一下書本第66頁的錯誤
wordList=textparse(open('email/ham/%d.txt' %i).read()
出現錯誤: UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence
網上百度錯誤原因: [python] view plain copy <pre name="code" class="python"> wordList = textParse(open('email/ham/%d.txt' % i).read())
在python3中讀取檔案時報錯:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multibyte sequence
網上各種資料大都顯示是檔案編碼問題,所以就把utf-8,gbk,asicc等各種編碼方式都試了一遍,還是沒有解決問題。
然後仔細看報錯資訊,根據decode byte 0xae in position 199看出來好像是檔案中某個位元組不能解碼,問題出來了,檔案中包含了非法字元。
開啟檔案一看,第二行中夾雜著“�”字元,這個字元本來是個普通問好“。”不知道什麼原因放入eclipse後就變了,刪除之後,一切就正常啦。
但是找了好久都沒有發現“�”字元,又百度發現解決辦法如下:
解決辦法:開啟email\ham\23.txt,找到SciFinance?,把?替換成空格即可。
但是然後又出現錯誤:
del(trainingSet[randIndex])
TypeError: 'range' object doesn't support item deletion
出現錯誤行為: del(trainingSet[randIndex])
原因是python3中range不返回數組對象,而是返回range對象
更改:
trainingSet = range(50);替換為trainingSet = list(range(50));