为什么以下项目失败?为什么它会成功使用"latin-1"编解码器?
o = "a test of \xe9 char" #I want this to remain a string as this is what I am receiving
v = o.decode("utf-8")
Run Code Online (Sandbox Code Playgroud)
结果是:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\encodings\utf_8.py",
line 16, in decode
return codecs.utf_8_decode(input, errors, True) UnicodeDecodeError:
'utf8' codec can't decode byte 0xe9 in position 10: invalid continuation byte
Run Code Online (Sandbox Code Playgroud) 我有一个网站,我可以在Firefox中使用jQuery发送我的土耳其语字符,但Internet Explorer不会发送我的土耳其语字符.我在记事本中查看了我的源文件,这个文件的代码页是ANSI.
当我将其转换为没有BOM的UTF-8并关闭文件时,当我重新打开时,该文件再次是ANSI.
如何将我的文件从ANSI转换为UTF-8?
我刚刚向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()
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
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)
Run Code Online (Sandbox Code Playgroud)
我觉得这很奇怪,因为据我所知,Python3应该支持utf-8无处不在.更重要的是,相同的代码在Python2.7上没有任何问题.我已经阅读了关于添加环境变量的内容PYTHONIOENCODING,但我尝试了它 …