Skip to content

Commit 0159588

Browse files
committed
committed from zkp
1 parent c490339 commit 0159588

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

FluentPython/clip.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def clip(text, max_len=80):
2+
# print(len(text))
3+
'''在max_len前面或者后面第一个出现的空格处截断文本'''
4+
end = None
5+
if len(text) > max_len:
6+
space_before = text.rfind(' ', 0, max_len)
7+
# print(space_before)
8+
if space_before >= 0:
9+
end = space_before
10+
else:
11+
space_after = text.rfind(" ", max_len)
12+
13+
if space_after >= 0:
14+
end = space_after
15+
if end is None:
16+
end = len(text)
17+
return text[:end].rstrip()

0 commit comments

Comments
 (0)