我正在分析一系列密码的频率。我的脚本正在处理其他输入媒体,但是当前数据集中似乎有一些错误字符。我怎样才能避开“坏”数据?
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
。您需要手动指定编码,我的最佳猜测是
latin-1
:如果您想在出现错误的情况下继续,可以将
errors='ignore'
或errors='replace'
传递给open
。相关问题 更多 >
编程相关推荐