(Feb-10-2020, 12:22 PM)Hobson Wrote: For various reasons I would prefer not to install anything that does not come as standardThis should nowadays not be a good reason at all,as
pip
come with Python an work in all kind of environments. There is
errors
parameter that can use,this will work but get some missing character.There is
ignore
or replace
.>>> s = 'Birleşik Krallık' >>> s.encode('iso-8859-1', errors='ignore') b'Birleik Krallk' >>> >>> s.encode('iso-8859-1', errors='replace') b'Birle?ik Krall?k'
Quote:If not then am I correct in thinking that if the UnicodeEncodeError occurs in your code I would need a second line of the type:Yes you only get in trouble because you try to encode to iso-8859-1,then it most be
text = text.decode()
to convert text from bytes to a string?
bytes
.To get back to sting most decode.
>>> s = 'hello' >>> s = s.encode() #Same as encode('utf-8') >>> s b'hello' >>> s.decode() #Same as decode('utf-8') 'hello' >>> s = 'hello' >>> s = s.encode('iso-8859-1') # Give it a other encoding than utf-8 >>> s b'hello' >>> s.decode('iso-8859-1') #Or just decode() would work in this case 'hello'Only see the difference if there is a Unicode character
>> s = 'helloø' >>> s = s.encode() >>> s b'hello\xc3\xb8' >>> >>> s = 'helloø' >>> s = s.encode('iso-8859-1') >>> s b'hello\xf8' >>> s.decode() # Now utf-8 back will fail Traceback (most recent call last): File "<interactive input>", line 1, in <module> UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 5: invalid start byte >>> s.decode('iso-8859-1') # Same encoding an it works 'helloø'