【发布时间】:2014-07-18 00:53:46
【问题描述】:
我刚刚在 Sublime 中添加了 Python3 解释器,以下代码停止工作:
for directory in directoryList:
fileList = os.listdir(directory)
for filename in fileList:
filename = os.path.join(directory, filename)
currentFile = open(filename, 'rt')
for line in currentFile: ##Here comes the exception.
currentLine = line.split(' ')
for word in currentLine:
if word.lower() not in bigBagOfWords:
bigBagOfWords.append(word.lower())
currentFile.close()
我收到以下异常:
File "/Users/Kuba/Desktop/DictionaryCreator.py", line 11, in <module>
for line in currentFile:
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcc in position 305: ordinal not in range(128)
我觉得这很奇怪,因为据我所知 Python3 应该在任何地方都支持 utf-8。更重要的是,相同的代码在 Python2.7 上没有问题。我已经阅读了有关添加环境变量PYTHONIOENCODING 的信息,但我尝试了它 - 无济于事(但是,在 OS X Mavericks 中添加环境变量似乎并不容易,所以也许我在添加变量时做错了? 我修改了/etc/launchd.conf)
【问题讨论】:
-
请务必包含您的异常的完整回溯。
标签: python python-3.x encoding