python3報錯解決辦法:UnicodeDecodeError: 'gbk' codec can't decode byte 0xae in position 199: illegal multib__python

來源:互聯網
上載者:User
在學習機器學習實戰這本書的時候,編譯書本上的程式時出現了很多的錯誤,現總結一下書本第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));  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.