Right-to-Left and Left-to-Right printed nicely

时间秒杀一切 提交于 2019-12-18 07:03:29

问题


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

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:

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


回答1:


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.)



来源:https://stackoverflow.com/questions/42556063/right-to-left-and-left-to-right-printed-nicely

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