Issue 29945: decode string:u"\ufffd" UnicodeEncodeError (original) (raw)
I use python on linux, version is 2.7.13:
[root@localhost bin]# ./python2.7 Python 2.7.13 (default, Mar 30 2017, 00:54:08) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2 Type "help", "copyright", "credits" or "license" for more information.
a=u"\ufffd" a.decode("utf=8") Traceback (most recent call last): File "", line 1, in File "/opt/python2.7.13/lib/python2.7/encodings/utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd' in position 0: ordinal not in range(128)
but,windows version run success!
windows version run success!
Trying to decode a non-ASCII unicode string should raise an exception on any platform:
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40)
[MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a=u"\ufffd"
>>> a.decode("utf=8")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Program Files\Python27\lib\encodings\utf_8.py",
line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\ufffd'
in position 0: ordinal not in range(128)
Unless you've modified the default encoding to something other than ASCII. For example:
Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40)
[MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, inspect, sitecustomize
>>> print inspect.getsource(sitecustomize)
import sys
sys.setdefaultencoding('utf-8')
>>> sys.getdefaultencoding()
'utf-8'
>>> a=u"\ufffd"
>>> a.decode("utf=8")
u'\ufffd'