-1

Reading from a database I get the following value

b'd\xe2\x80\x99int'

How can I print it to get the string d’int (note that this is different from d'int)?

I tried with print(b'd\xe2\x80\x99int'.decode('utf-8')) but I get the error:

UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 1: ordinal not in range(128)

EDIT: thanks to the comment I understood that the problem is not in my Python code but in emacs, I am having exactly the same problem as described here Unicode conversion issue using Python in Emacs

I will close the question

Nisba
  • 3,210
  • 2
  • 27
  • 46
  • Your code works for me, without error. – Colin Ricardo Apr 22 '18 at 14:23
  • @Nisba How can we reproduce your error? This line `print(b'd\xe2\x80\x99int'.decode('utf-8'))` won't raise any exception – BPL Apr 22 '18 at 14:25
  • You are right, in my terminal it works. I get the error running it inside emacs, either if I print it on the shell console inside emacs either in a file. So maybe my Python process in emacs must be told to accept unicode strings – Nisba Apr 22 '18 at 14:28
  • @Nisba Does set `PYTHONIOENCODING` env.var to `utf-8` makes any difference when spawning python process on your emacs console or whatever emacs uses to display text? – BPL Apr 22 '18 at 14:37
  • I am trying to change its value – Nisba Apr 22 '18 at 14:47

1 Answers1

0

You can use bytes.decode()

>>> bytes.decode(b'd\xe2\x80\x99int', 'utf8')
'd’int'

or analogously, the .decode() method over the bytes object itself:

>>> b'd\xe2\x80\x99int'.decode('utf-8')
'd’int'
fferri
  • 18,285
  • 5
  • 46
  • 95