Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to print Arabic text correctly in PYTHON

I am using Python 2.7 and i try to print Arabic strings like these

print "ذهب الطالب الى المدرسة"

it's give the following output:

ط°ظ‡ط¨ ط§ظ„ط·ط§ظ„ط¨ ط§ظ„ظ‰ ط§ظ„ظ…ط¯ط±ط³ط©

The purpose is to print the text correctly, and not how to print each line. So, how can I print the string or content of text file correctly in its original form? like:

ذهب الطالب الى المدرسة
like image 300
Mohammed Sy Avatar asked Dec 20 '16 13:12

Mohammed Sy


1 Answers

by this module you can correct your text shape an direction. just install pips and use it.

# install: pip install --upgrade arabic-reshaper
import arabic_reshaper

# install: pip install python-bidi
from bidi.algorithm import get_display

text = "ذهب الطالب الى المدرسة"
reshaped_text = arabic_reshaper.reshape(text)    # correct its shape
bidi_text = get_display(reshaped_text)           # correct its direction
like image 68
Jalal Razavi Avatar answered Oct 14 '22 03:10

Jalal Razavi