使用 Python 将阿拉伯语或任何从右到左书写系统字符串打印到 Linux 终端 [英] Print Arabic or any Right-to-Left writing-system string to Linux terminal using Python

查看:55
本文介绍了使用 Python 将阿拉伯语或任何从右到左书写系统字符串打印到 Linux 终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

非常简单的例子是:

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

我期望输出为:

‏المكلا‎

但实际上输出是反向字符串(字母看起来有点不同,因为它们有开头、中间和结尾的形式).我不能在这里粘贴它,因为复制粘贴再次纠正了字符串的顺序.

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.

如何将阿拉伯语正确打印到 Linux 终端?周围的文本是从左到右 (LTR),只有这一行需要从右到左 (RTL).是否有 UFT-8 字符可以告诉终端?

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?

推荐答案

使用 RTL 字符创建字符串:

To create a string with the RTL character:

rtl = u'\u200f'

Python 3 默认使用 UTF 字符串,因此在这种情况下,字符串前面的u"将是不必要的.

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'

还有 python-bidi 库 可能会有所帮助.(来源)

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

这篇关于使用 Python 将阿拉伯语或任何从右到左书写系统字符串打印到 Linux 终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆