-->

how to print Arabic text correctly in PYTHON

2019-06-23 14:17发布

问题:

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:

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

回答1:

Try this:

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

Output:

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

Demo: https://repl.it/EuHM/0

The default Python2.7 string works with utf-8 character set. And arabic is not included inside utf-8. So if you prefix it with u then it will treat that string as unicode string.



回答2:

You need to add some line before your code

import sys
reload(sys)
sys.setdefaultencoding('utf-8')  
print "ذهب الطالب الى المدرسة"


回答3:

You can either prefix your string with u like this

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

or make yourself compatible with python3 and put this in the top of your file

from __future__ import unicode_literals

Python27 strings (or bytestrings as they're known in Python3) do not handle unicode characters. Both the u and the import statement make your string unicode compatible.



回答4:

In python 2.7

at the very top of your file you can declare:

# -*- coding: utf-8 -*-
print "ذهب الطالب الى المدرسة"

Updated:

If you can run this:

# -*- coding: utf-8 -*-
s = "ذهب الطالب الى المدرسة"
with open("file.txt", "w", encoding="utf-8") as myfile:
    myfile.write(s)

And the file generated "file.txt" contains the correct string then it is a problem with whatever you are displaying in in not python itself, I guess you could try displaying it in something else, maybe even PyQt.



回答5:

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