how to print Arabic text correctly in PYTHON

倾然丶 夕夏残阳落幕 提交于 2020-01-03 11:30:13

问题


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:


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



回答3:


You need to add some line before your code

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



回答4:


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.




回答5:


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.




回答6:


The following code works:

import arabic_reshaper

text_to_be_reshaped =  'اللغة العربية رائعة'

reshaped_text = arabic_reshaper.reshape(text_to_be_reshaped)

rev_text = reshaped_text[::-1]  # slice backwards 

print(rev_text)


来源:https://stackoverflow.com/questions/41243215/how-to-print-arabic-text-correctly-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!