0
votes

Why the following code is still use "ascii" to decode the string. Didn't I tell python to use "utf-8" to decode the string? Plus, how come ignore did not work?

print data.encode('utf-8', 'ignore')

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 12355:

1
How you explicitly tell Python to handle a string does not affect what it does implicitly in order to print it. - Ignacio Vazquez-Abrams
@IgnacioVazquez-Abrams I dont think its the print ... see my answer (I think its right... I dunno string encodings sometimes hang me up too) - Joran Beasley

1 Answers

3
votes

I assume data is a str

print isinstance(data,str)

should probably tell you true

encode wants a unicode so first it tries to decode your str to unicode using the ascii codec

hence why you get the UnicodeDecodeError not UnicodeEncodeError

try

print data.decode("utf-8","ignore")