Python3: An Example String That Would Cause Unicodeencodeerror
I am trying to find a string, that would raise UnicodeEncodeError: dirty_str = 'FC Bayern München' # or anything else possible dirty_str.encode('utf-8') Whatever I have given t
Solution 1:
Surrogates (D800—DBFF and DC00—DFFF) will raise when encoded:
>>> '\ud83d\udca9'.encode()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError:'utf-8' codec can't encode characters in position 0-1: surrogates not allowed
However, since you are getting the string from input()
, there is no UTF-8 string you can input that will decode to these code points. (On Linux systems, though, Python will use these to represent invalid UTF-8 bytes in path and file names.)
Post a Comment for "Python3: An Example String That Would Cause Unicodeencodeerror"