在python中的空格中x字符后切割字符串

时间:2013-09-09 07:47:24

标签: python

我想在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'

最简单的方法是什么?

3 个答案:

答案 0 :(得分:20)

import textwrap
lines = textwrap.wrap(text, 20)
# then use either
lines[0]
# or
'\n'.join(lines)

答案 1 :(得分:7)

您可以使用str.rpartition()str.rsplit()删除余下的最后一个空格后的所有内容:

example[:20].rpartition(' ')[0]

example[:20].rsplit(' ', 1)[0]

str.rsplit()的第二个参数将分割限制为右边的第一个空格,而[0]索引则取消在该空格之前拆分的任何内容。

str.rpartition()有点快,总是返回三个字符串;如果没有空格,那么返回的第一个字符串是空的,所以如果可能的话,你可能想要坚持str.rsplit()(在那种情况下该版本会返回一个包含单个字符串的列表,所以你最终会得到原来的字符串)。

答案 2 :(得分:2)

赞成另外两个答案,但只是为了好玩,使用正则表达式:

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
''
''