Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

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

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('
'+'-'*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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...