0

Do someone know why my python 3 terminal does not show unicode properly ?

text= "בראשית ברא אלהים את השמים ואת הארץ".encode("UTF-8")
print (text)

but the output is :

b'\xd7\x91\xd7\xa8\xd7\x90\xd7\xa9\xd7\x99\xd7\xaa \xd7\x91\xd7\xa8\xd7\x90 \xd7\x90\xd7\x9c\xd7\x94\xd7\x99\xd7\x9d \xd7\x90\xd7\xaa \xd7\x94\xd7\xa9\xd7\x9e\xd7\x99\xd7\x9d \xd7\x95\xd7\x90\xd7\xaa \xd7\x94\xd7\x90\xd7\xa8\xd7\xa5'

How can to print it properly ?

or if i dont encode it :

text= "בראשית ברא אלהים את השמים ואת הארץ"
print (text)

this error comes out:

return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: character maps to

oj pr
  • 1
  • 2
  • Have you seen [this](https://stackoverflow.com/questions/29850912/decoding-and-encoding-hebrew-string-in-python)? I think that can help you. – DeusDev Jul 17 '21 at 00:53
  • This is OS-specifc, but for Windows you can install Hebrew language support and then switch to a Hebrew-supporting font in the CMD window. I've done this with Chinese and it works great. See [this](https://stackoverflow.com/a/63893448/235698). – Mark Tolonen Jul 27 '21 at 23:21

2 Answers2

0

You can either avoid encoding, or encode and then decode.

In [1]: text= "בראשית ברא אלהים את השמים ואת הארץ"

In [2]: print(text)
בראשית ברא אלהים את השמים ואת הארץ

In [3]: encoded = text.encode('utf8')

In [4]: decoded = encoded.decode('utf8')

In [5]: print(decoded)
בראשית ברא אלהים את השמים ואת הארץ
andi
  • 41
  • 1
  • 2
0

text is a bytes value. print takes str values; str(b'foo') is not 'foo', but "b'foo'".

Don't encode text in the first place. It's already a suitable value to use as an argument for print. Any encoding needed for your terminal is done by the file-like object print writes to.

text = "בראשית ברא אלהים את השמים ואת הארץ"
print(text)
chepner
  • 497,756
  • 71
  • 530
  • 681