Print Arabic or any Right-to-Left writing-system string to Linux terminal using Python

北城以北 提交于 2020-01-03 17:07:07

问题


The very simple example is:

city = "‏المكلا‎"
print(city)

I am expecting the output to be:

‏المكلا‎

But in fact the ouput is the reverse string (the letters look a little different because they have a start-, middle- and end-form). I can't paste it here, because copy-pasting corrects the order of the string again.

How can I print Arabic correctly to the Linux terminal? The surounding text is left-to-right (LTR) and only this line needs to be right-to-left (RTL). Is there a UFT-8 character that can tell ther terminal that?


回答1:


To create a string with the RTL character:

rtl = u'\u200f'

Python 3 uses UTF strings by default, so in that case the "u" in front of the string would be unnecessary.

If the problem is actually that the terminal just can't render correctly, you could manually reverse the string.

test = 'Hello world'
test = test[::-1]
# test == 'dlroW olleH'

There is also the python-bidi library which might be able to help. (source)



来源:https://stackoverflow.com/questions/33700735/print-arabic-or-any-right-to-left-writing-system-string-to-linux-terminal-using

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