0

I want to encode an Arabic string. I actually tried to pass the string as is, but it did not work. I tried to encode it and it also didn't work. Here is the code and the output: جاÙ\x85عة اÙ\x84Ù\x8aرÙ\x85Ù\x88Ù\x83 this is the output

self.set_font("Arial","",11)

self.set_text_color(15,164,12)
self.set_y(2.0)
str="جامعة اليرموك"
str=str.encode("utf-8")
str=str.decode("latin1")

self.cell(0,5,str,align="C",border=1)

I was expecting the output is "جامعة اليرموك"

JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Does the pyfpdf example for Unicode text work for you? https://pyfpdf.readthedocs.io/en/latest/Unicode/index.html – Nick ODell Nov 17 '22 at 17:08
  • even in the result folder that made by fpdf is showing correct the output :"ملاعلا ابحرم :Arabic" the letters reversed – Awos Al-Radaideh Nov 17 '22 at 17:12
  • Don't think that the `str` will work, since it's a reserved python name. – Koti Nov 17 '22 at 17:24
  • **1st** stop overriding built-in names like [Text Sequence Type — `str`](https://docs.python.org/3.10/library/stdtypes.html#text-sequence-type-str). **2nd** why do you expect `a_str.encode('utf-8').decode('latin1') == a_str`? This is valid merely in ASCII range… – JosefZ Nov 17 '22 at 17:30

1 Answers1

0

If you're using Python3, you don't need to encode or decode anything. Strings are unicode by default:

>>> title = "جامعة المبروك" 
>>> print(title)
جامعة المبروك

As mentioned in the comments, you shouldn't use str as a variable name, because it's a built-in function in Python. (You can tell it's a built-in function because it's red in your code.) Built-in functions are reserved, and overwriting them can cause unpredictable behavior.

But that's not actually your problem here. The code where you're trying to en-/decode is what's causing the jibberish:

>>> title = "جامعة المبروك"
>>> title = title.encode('utf-8')
>>> title = title.decode('latin1')
>>> print(title)
Ø¬Ø§ÙØ¹Ø© اÙÙØ¨Ø±ÙÙ

Just take that out (and change your variable name) and you should be fine.

حظ سعيد

larapsodia
  • 594
  • 4
  • 15