How does python handle and print strings that contains RTL and LTR languages?
Asked Answered
A

1

8

I have been searching the web and asking people about this subject but I really did not get a satisfying answer, let me explain my issue here with python.

let's say I have this string:

a = 'حصل طالب على (80) في (something) واجتاز الشرط بنجاح'

the string above should actually look like:

b = 'واجتاز الشرط بنجاح (something) حصل طالب على (80) في'

because it starts at ( حصل ) and ends at ( بنجاح )

keep in mind that the contents of variable a are written in order and correctly but python SADLY prints as it sees, so the output would be incorrect although the writing was correct it got flipped when ( (something) ) got in the string, so variable b contents are a workaround method to get the text in the right shape although it was written wrongly and not in the order that it should be written!

now, you may say "then what are you blabbing about if you found a way to get it to work?! :S", well as I found out a way to get the text in shape and to look good and readable, I discovered that python has more to it than "python prints what it sees" python actually behave in a really strange way when it's trying to print a bidirectional string, it literally jumps around the string in order to print it, it has no starting point and ending point (meaning print from the right to the left or from the left to the right) let's take variable b string for example:

instead of printing the string from the right as the starting point to the left as the ending point (as it should be),

python will print ( واجتاز الشرط بنجاح ) first, then ( (something) ) second, then ( حصل طالب على (80) في ) third and the final string printed will be ( واجتاز الشرط بنجاح (something) حصل طالب على (80) في ) which will give you the illusion that python printed your string as you see it (from right to left)

now that you understand the whole situation, I am going to tell you what I need python to do, which is printing the string in order as java does, using variable a which is the correct written variable but as I explained above, python is not smart enough to do what it should (java knows where you started your string and where you ended it and it uses these two points to print out your string surprisingly true and correct).

please help :)

Auriga answered 4/8, 2020 at 9:6 Comment(2)
please read this answer: #42556563Rippy
or read this article: unicode.org/reports/tr9Rippy
R
0

This is not a Python issue, it is a shell terminal issue.

Either use:

  • A terminal emulator that support Unicode Bidirectional & Shaping
  • Or use python library like FreeBidi (Bidi + Basic shaping)
Ry answered 25/12, 2021 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.