1
我試圖分析一系列頻率的密碼。我的腳本正在處理其他輸入媒體,但是看起來我的當前數據集中存在一些不好的字符。我怎樣才能解決「壞」數據?字符串處理錯誤:UnicodeDecodeError:'utf8'編解碼器無法解碼
import re
import collections
words = re.findall('\w+', open('rockyou.txt').read().lower())
a=collections.Counter(words).most_common(50)
for word in a:
print(word)
然後我得到的錯誤:
Traceback (most recent call last):
File "shakecount.py", line 3, in <module>
words = re.findall('\w+', open('rockyou.txt').read().lower().ASCII)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf1 in position 5079963: invalid continuation byte
任何想法?
以上是有益的,但並沒有最終解決問題,我跑到更多的希臘錯誤(我是編程新手)。我最終在文本編輯器中打開了單詞列表,並重新編譯爲utf-8格式,然後運行。感謝agf的幫助! – AlphaTested 2012-04-12 07:01:07
@AlphaTested如果你不知道編碼,另一種方法是使用[chardet](http://pypi.python.org/pypi/chardet)來檢測它。 – agf 2012-04-12 07:04:00
啊,我明白了。謝謝。 – AlphaTested 2012-04-12 07:37:42