0

I try to print a stirng and for some reason it's turn into gibrish. But if I put it in an online utf 8 convertr it works (https://www.browserling.com/tools/utf8-decode). And the output i get is: "תיכון דה שליט" instead of: "תיכוןדהשליט" that I get in the website and is the orignal stirg.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
print '\xd7\xaa\xd7\x99\xd7\x9b\xd7\x95\xd7\x9f \xd7\x93\xd7\x94 \xd7\xa9\xd7\x9c\xd7\x99\xd7\x98'
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • The output from the website I had in the link is the correct output and for someone reason I get the other resulet(I will edit my question to make it clearer). –  Feb 28 '18 at 23:37
  • `print my_string.decode('utf8')` – Joran Beasley Feb 28 '18 at 23:45
  • Yep, I you are right kindda my bad I didnt get the whole conept of encoding right and thought that I need to encode into utf8 or something like this. –  Feb 28 '18 at 23:46
  • I can't reproduce in python 2.7: https://repl.it/repls/CompetitiveVictoriousDiskdrive – user3483203 Feb 28 '18 at 23:47
  • @chrisz he is running it in his terminal I think which is not decoding it with utf8 but with something else(latin1 i think) ... (repl.it is probably doing some encoding mojo behind the scenes) – Joran Beasley Feb 28 '18 at 23:50
  • So what are you printing **to**? This isn't a Python problem, you have valid UTF-8. But your *terminal* or *console* must be configured to expect UTF-8 for this to work. – Martijn Pieters Mar 01 '18 at 07:44

1 Answers1

0

you just need to decode the bytes back into unicode with utf8 encoding ... by default its using whatever your terminal is using(probably latin1) ... your terminal may not support all of the characters * (see https://repl.it/repls/OnlyRewardingKeyboardmacro)

print '\xd7\xaa\xd7\x99\xd7\x9b\xd7\x95\xd7\x9f \xd7\x93\xd7\x94 \xd7\xa9\xd7\x9c\xd7\x99\xd7\x98'.decode('utf8')

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179