Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

utf-8 plus question marks

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.

like image 581
primroot Avatar asked Apr 08 '26 19:04

primroot


1 Answers

Try:

inputstring.decode("utf8", "replace")

See here for reference

like image 198
Joril Avatar answered Apr 10 '26 10:04

Joril



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!