在 Python 中对齐两侧的文本
Align text for both sides in Python
我知道函数 print()
可以 align text to right or to left,但我可以向两侧对齐吗?
示例:
本文:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit.
应该是这样的:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit.
没有办法做到这一点,我知道这是内置的。但是你可以尝试自己解决它:
一种可能的(不太好看)算法:
- 将文本拆分成行
- 找到最长的线
- 对于所有其他行:
- 首先找到space,然后添加另一个space
- 如果还不够长,移到下一个还没拉长的space
这可能是这样的:
def justify_text(text):
# split text into lines and find longest
lines = [line.strip() for line in text.split("\n")]
ll = len(max(lines, key=len))
# iterate lines
for i, l in enumerate(lines):
# remember last elongates space
pos_space = 0
# do for all lines but the last one
while len(l) < ll and (i != len(lines)-1):
# print(l) # uncomment to see stages of refining
pos_space = l.find(" ", pos_space)
if pos_space == -1:
# start over from beginning
pos_space = l.find(" ", 0)
if pos_space == -1:
# no space inside the line, can't do anything about it
# we break to avoid endless loop
break
# splice in a space and increase next search position
l = l[:pos_space] + " " + l[pos_space:]
pos_space += 2
# store changed line
lines[i] = l
return '\n'.join(lines)
t = """Lorem ipsum dolor sit amet, consectetur adipiscing more text
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit."""
print(justify_text(t))
并产生:
Lorem ipsum dolor sit amet, consectetur adipiscing more text
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit.
要更接近您的文本的外观,您必须找到 space 以从行的两侧交替搜索中拉长。
如果您不从已经分行的文本开始,您可以进行预处理(尽管在这种情况下使用 linked algo from codereview 效率更高):
t = """Lorem ipsum dolor sit amet, consectetur adipiscing more text elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit."""
def prep_text(text, no_longer_then=50):
words = text.split(" ")
result = [[]]
for w in words:
if sum(len(part) for part in result[-1]) + len(w) > no_longer_then:
result.append([w])
else:
result[-1].append(w)
print(result)
return '\n'.join(' '.join(inner) for inner in result)
prepped = prep_text(t)
print(prepped)
输出:
# 1 2 3 4 5
# 345678901234567890123456789012345678901234567890
Lorem ipsum dolor sit amet, consectetur adipiscing
more text elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit.
我在这里写了我自己的版本(当很多space行时也有一个用“-”填充):
https://github.com/zvibazak/both_sides_align
用法:
both_sides_align(txt, size, max_spaces=5)
txt
- 要对齐的文本
size
- 输出大小
max_spaces
- 最多 space 添加到一行(在此数字之上 - 将添加一个“-”并将单词拆分到下一行)
示例:
使用此代码
from both_sides_align import both_sides_align
both_sides_align(txt,60,3)
将给出此文本:
Nulla luctus bibendum nulla nec efficitur. Quisque id aliq-
uam enim. Etiam non lectus id risus rhoncus condimentum.
Nam ultrices ex quis risus iaculis ullamcorper. Vivamus id
venenatis mi, et suscipit ipsum.```
我知道函数 print()
可以 align text to right or to left,但我可以向两侧对齐吗?
示例: 本文:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit.
应该是这样的:
Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit.
没有办法做到这一点,我知道这是内置的。但是你可以尝试自己解决它:
一种可能的(不太好看)算法:
- 将文本拆分成行
- 找到最长的线
- 对于所有其他行:
- 首先找到space,然后添加另一个space
- 如果还不够长,移到下一个还没拉长的space
这可能是这样的:
def justify_text(text):
# split text into lines and find longest
lines = [line.strip() for line in text.split("\n")]
ll = len(max(lines, key=len))
# iterate lines
for i, l in enumerate(lines):
# remember last elongates space
pos_space = 0
# do for all lines but the last one
while len(l) < ll and (i != len(lines)-1):
# print(l) # uncomment to see stages of refining
pos_space = l.find(" ", pos_space)
if pos_space == -1:
# start over from beginning
pos_space = l.find(" ", 0)
if pos_space == -1:
# no space inside the line, can't do anything about it
# we break to avoid endless loop
break
# splice in a space and increase next search position
l = l[:pos_space] + " " + l[pos_space:]
pos_space += 2
# store changed line
lines[i] = l
return '\n'.join(lines)
t = """Lorem ipsum dolor sit amet, consectetur adipiscing more text
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit."""
print(justify_text(t))
并产生:
Lorem ipsum dolor sit amet, consectetur adipiscing more text
elit, sed do eiusmod tempor incididunt ut labore
et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure
dolor in reprehenderit.
要更接近您的文本的外观,您必须找到 space 以从行的两侧交替搜索中拉长。
如果您不从已经分行的文本开始,您可以进行预处理(尽管在这种情况下使用 linked algo from codereview 效率更高):
t = """Lorem ipsum dolor sit amet, consectetur adipiscing more text elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit."""
def prep_text(text, no_longer_then=50):
words = text.split(" ")
result = [[]]
for w in words:
if sum(len(part) for part in result[-1]) + len(w) > no_longer_then:
result.append([w])
else:
result[-1].append(w)
print(result)
return '\n'.join(' '.join(inner) for inner in result)
prepped = prep_text(t)
print(prepped)
输出:
# 1 2 3 4 5
# 345678901234567890123456789012345678901234567890
Lorem ipsum dolor sit amet, consectetur adipiscing
more text elit, sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua. Ut enim ad minim
veniam, quis nostrud exercitation ullamco laboris
nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit.
我在这里写了我自己的版本(当很多space行时也有一个用“-”填充): https://github.com/zvibazak/both_sides_align
用法:
both_sides_align(txt, size, max_spaces=5)
txt
- 要对齐的文本
size
- 输出大小
max_spaces
- 最多 space 添加到一行(在此数字之上 - 将添加一个“-”并将单词拆分到下一行)
示例:
使用此代码
from both_sides_align import both_sides_align
both_sides_align(txt,60,3)
将给出此文本:
Nulla luctus bibendum nulla nec efficitur. Quisque id aliq-
uam enim. Etiam non lectus id risus rhoncus condimentum.
Nam ultrices ex quis risus iaculis ullamcorper. Vivamus id
venenatis mi, et suscipit ipsum.```