Reputation: 1686
I have a site that displays user input by decoding it to unicode using utf-8. However, user input can include binary data, which is obviously not always able to be 'decoded' by utf-8.
I'm using Python, and I get an error saying:
'utf8' codec can't decode byte 0xbf in position 0: unexpected code byte. You passed in '\xbf\xcd...
Is there a standard efficient way to convert those undecodable characters into question marks?
It would be most helpful if the answer uses Python.
Upvotes: 3
Views: 2178
Reputation: 14185
I think what you are looking for is:
str.decode('utf8','ignore')
which should drop invalid bytes rather than raising exception
Upvotes: 1