我想在x个字符之后剪切一个长文本,但我不想在中间剪切一个单词,我想在x个字符之前的最后一个空格处剪切:
'This is a sample text'[:20]
给我
'This is a sample tex'
但我想要
'This is a sample'
另一个例子:
'And another sample sentence'[:15]
给我
'And another sam'
但我想要
'And another'
最简单的方法是什么?
我想在x个字符之后剪切一个长文本,但我不想在中间剪切一个单词,我想在x个字符之前的最后一个空格处剪切:
'This is a sample text'[:20]
给我
'This is a sample tex'
但我想要
'This is a sample'
另一个例子:
'And another sample sentence'[:15]
给我
'And another sam'
但我想要
'And another'
最简单的方法是什么?
import textwrap
lines = textwrap.wrap(text, 20)
# then use either
lines[0]
# or
'\n'.join(lines)
您可以使用str.rpartition()或str.rsplit()删除余数的最后一个空格之后的所有内容:
example[:20].rpartition(' ')[0]
example[:20].rsplit(' ', 1)[0]
第二个参数str.rsplit()将拆分限制为从右侧开始的第一个空格,并且[0]索引采用在该空格之前拆分的任何内容。
str.rpartition()快一点,而且总是返回三个字符串;如果没有空格,则返回的第一个字符串为空,因此如果有可能,您可能要坚持使用str.rsplit()(在这种情况下,该版本将返回带有单个字符串的列表,因此您最终会再次使用原始字符串)。
赞成其他两个答案,但只是为了好玩,使用正则表达式:
import re
r = re.compile('.{,20}(?<! )(?= |\Z|\A)')
for s in ('This is a sample text',
'abcdefghijklmnopqrstuvwxyz ',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
'This is 1 first sample text ',
'This is 1 again sample text',
'A great blank here',
'Another blank here',
'A short text',
' little indent',
' great indent',
'ocean',
'!',
''):
print ('-----------------------\n'
" ....5...10...15...20\n"
'%r\n%r'
% (s, r.match(s).group() ) )
结果
-----------------------
....5...10...15...20
'This is a sample text'
'This is a sample'
-----------------------
....5...10...15...20
'abcdefghijklmnopqrstuvwxyz '
''
-----------------------
....5...10...15...20
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
''
-----------------------
....5...10...15...20
'This is 1 first sample text '
'This is 1 first'
-----------------------
....5...10...15...20
'This is 1 again sample text'
'This is 1 again'
-----------------------
....5...10...15...20
'A great blank here'
'A great blank'
-----------------------
....5...10...15...20
'Another blank here'
'Another blank'
-----------------------
....5...10...15...20
'A short text'
'A short text'
-----------------------
....5...10...15...20
' little indent'
' little indent'
-----------------------
....5...10...15...20
' great indent'
''
-----------------------
....5...10...15...20
'ocean'
'ocean'
-----------------------
....5...10...15...20
'!'
'!'
-----------------------
....5...10...15...20
''
''