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