简体   繁体   English

在Python中平均分隔单词

[英]Equally separating words in Python

What I want to do is separate chunks of text so that I have two chunks in each line and in the different lines they begin at the same point. 我想要做的是分开的文本块,这样我在每一行中都有两个大块,并且在不同的行中它们从同一点开始。 What I am using it for is a little book manager program I am developing for my own usage, so it should look something like this: 我正在使用的是一个小书管理器程序,我正在为自己的使用而开发,因此它看起来应该像这样:

Book Title Here                    Author Name Here
Little longer title here           Author Name Here
shorter here                       Author Name Here

I tried making use of .ljust() or .rjust() using spaces, but it didn't really work for me: for whatever reason, the spaces would not even out and I ended up not having the titles stacked up together, but rather separated by very little. 我尝试使用带空格的.ljust().rjust() ,但对我而言实际上并不起作用:无论出于何种原因,这些空格都不会均匀出现,并且最终我没有将标题叠加在一起,但是相隔甚远。

I am using Tkinter to build the GUI, and each line should be an item on a list box. 我正在使用Tkinter来构建GUI,并且每一行都应该是列表框中的一项。

I would recommend using the format mini-language , here's the setup: 我建议使用mini-language格式 ,这是设置:

bookdict = {
  'Little longer title here': 'Author Name Here',
  'Book Title Here': 'Another Author Name Here',
  'shorter here': 'Diff Name Here'}
bookwidth = max(map(len, bookdict.keys()))
authorwidth = max(map(len, bookdict.values()))

format mini-language format迷你语言

And this usage of the mini-language: 迷你语言的用法如下:

template = '{{0:<{bw}}} {{1:>{aw}}}'.format(bw=bookwidth, aw=authorwidth)
for book, author in bookdict.items():
    print( template.format(book, author) )

prints: 打印:

Little longer title here         Author Name Here
Book Title Here          Another Author Name Here
shorter here                       Diff Name Here

To break that down, the doubled braces will be preserved on the first format and reduced to single braces, and the single braces will become the width of the max lens calculated, eg: 为了解决这个问题,双括号将保留在第一种格式,并减少为单括号,并且单括号将成为计算出的最大镜头的宽度,例如:

'{0:<30} {1:>20}'

The less-than ( < ) means justify left, and the greater-than ( > ) means justify right. 小于( < )表示左对齐,大于( > )表示右对齐。

rjust and ljust rjustljust

If you really want to use the str.rjust and str.ljust methods: 如果您真的想使用str.rjust和str.ljust方法:

for book, author in bookdict.items():
    print(book.ljust(bookwidth) + ' ' + author.rjust(authorwidth))

prints: 打印:

Little longer title here         Author Name Here
shorter here                       Diff Name Here
Book Title Here          Another Author Name Here

If you are using a fixed-width font, then "{:40}{}".format("Book Title Here", "Author Name Here" is your friend. (Change 40 to however many spaces you want to allocate for the first part.) 如果您使用的是固定宽度的字体,则"{:40}{}".format("Book Title Here", "Author Name Here"是您的朋友。(将40更改为要为第一部分。)

If you are using a variable-width font, then you'll want to do this using Tkinter's ways of arranging things, which would likely boil down to putting the two parts of each line in their own sections. 如果您使用的是可变宽度字体,则需要使用Tkinter的排列方式来实现,这很可能归结为将每行的两部分放在各自的部分中。

For instance, you could do something along the lines of: 例如,您可以按照以下方式进行操作:

Label(master, text="Book Title Here").grid(row=0, sticky=W)
Label(master, text="Author Name Here").grid(row=0, column=1, sticky=W)

Label(master, text="Little longer title here").grid(row=1, sticky=W)
Label(master, text="Author Name Here").grid(row=1, column=1, sticky=W)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM