Форум сайта python.su
22
привет.
есть строка
a=u'здесь очень длинный текст который надо разбить'
list=[u'здесь очень длинный', u'текст который надо', u'разбить']
Офлайн
52
a = 'We use cookies to ensure that we give you the best experience on our website. We also use cookies to ensure we show you advertising that is relevant to you. If you continue without changing your settings, we\'ll assume that you are happy to receive all cookies on the BBC website. However, if you would like to, you can change your cookie settings at any time.' result = [] bufstr = '' for dig in a: bufstr += dig if len(bufstr) > 23: to_append, bufstr = bufstr.rsplit(' ', 1) result.append(to_append) result.append(bufstr) print map(len, result), result
[17, 23, 22, 20, 21, 23, 19, 20, 21, 22, 21, 22, 16, 21, 23, 23, 9] ['We use cookies to', 'ensure that we give you', 'the best experience on', 'our website. We also', 'use cookies to ensure', 'we show you advertising', 'that is relevant to', 'you. If you continue', 'without changing your', "settings, we'll assume", 'that you are happy to', 'receive all cookies on', 'the BBC website.', 'However, if you would', 'like to, you can change', 'your cookie settings at', 'any time.']
Отредактировано fata1ex (Июль 22, 2012 10:44:06)
Офлайн
22
to_append, bufstr = bufstr.rsplit(' ', 1)
Офлайн
52
Офлайн
36
А чем модуль textwrap не угодил?
Офлайн
1
наш пострел везде поспел :)
Офлайн
22
Virtuos86т.е.?
наш пострел везде поспел :)
Офлайн