Right-to-Left and Left-to-Right printed nicely
Asked Answered
E

2

10

I want it to produce the number next to a word so that I can ask the user to select the word by using the corresponding number.

This is my code

alt_words = hlst
loopnum = 8
for i in range(loopnum):
        if i < len(alt_words):
            print('{0}. {1:<20}'.format((i+1), alt_words[i]), end =' ')
            if i == 0:
                print('', end=' ')
        if i + 9 <= len(alt_words):
            print('{0}. {1:<20}'.format((i+9), alt_words[i+8]), end =' ')
        if i + 17 <= len(alt_words):
            print('{0}.  {1:<20}'.format((i+17), alt_words[i+16]), end=' ')
        print('\n'+'-'*80)

It produces this Output from code

The first number of each line gets printed on the left, but the word on the right, while the rest of the numbers and words get printed RTL. It seems that once python has started printing on a line LTR it can switch to RTL, but not back from RTL to LTR. Note how even the periods are printed to the right of the number for the second set of numbers on each line.

It works perfectly well and looks nice with english words:

python table works in english

I am guessing a work around might involve putting the number after the word, but I figure there must be a better way.

Epinasty answered 2/3, 2017 at 12:53 Comment(3)
Did you tried this ?Barth
Nice rabbit hole. The main answer says "have you tried this..."Norton
The actual words on being displayed correctly so I am not sure that it is relevant.Epinasty
H
13

Put a Right-to-Left Embedding character, u'\u202B', at the beginning of each Hebrew word, and a Pop Directional Formatting character, u'\u202C', at the end of each word.

This will set the Hebrew words apart as RTL sections in an otherwise LTR document.

(Note that while this will produce the correct output, you're also dependent on the terminal application in which you're running this script having implemented the Unicode Bidirectional Algorithm correctly.)

Hite answered 2/3, 2017 at 14:5 Comment(2)
sometimes (e.g. In Jupyter, globally setup for LTR) it is enough to prepend the RTL control character from above to every word be the word RTL or LTR.Pentagrid
I think you want u'\u2067' RIGHT-TO-LEFT ISOLATE and u'\u2069' POP DIRECTIONAL ISOLATE, not RIGHT-TO-LEFT EMBEDDING and POP DIRECTIONAL FORMATTING. A right-to-left directional embedding has roughly the effect of a strong RTL character on the surrounding text, which can cause undesirable reordering of text you wanted to isolate from the effects of the RTL characters inside the embedding.Cavatina
L
6

See Bi-directional (BiDi) layout implementation in pure python.

Install with:

pip install python-bidi

Example usage:

from bidi.algorithm import get_display
print(get_display('LTR text with RTL text (טקסט לדוגמא) will be printed correctly'))

The following package is also available if you are using Django: http://pypi.python.org/pypi/django-bidi-utils

Loaves answered 23/1, 2021 at 18:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.