将字符串拆分为最大长度为X的片段-仅在空格处拆分 [英] Split a string into pieces of max length X - split only at spaces

查看:82
本文介绍了将字符串拆分为最大长度为X的片段-仅在空格处拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的字符串,我希望将其分解为最多X个字符。但是,只能在一个空格处(如果字符串中的某个单词长于X个字符,则将其放入自己的片段中。)

I have a long string which I would like to break into pieces, of max X characters. BUT, only at a space (if some word in the string is longer than X chars, just put it into its own piece).

我什至不知道如何开始使用Python ...

I don't even know how to begin to do this ... Pythonically

伪代码:

declare a list
while still some string left:
   take the fist X chars of the string
   find the last space in that
   write everything before the space to a new list entry
   delete everything to the left of the space

在我对此进行编码之前,是否有一些内容可以帮助我的python模块(我认为 pprint 可以)?

Before I code that up, is there some python module that can help me (I don't think that pprint can)?

推荐答案

使用 textwrap 模块(也会在连字符处断开):

Use the textwrap module (it will also break on hyphens):

import textwrap
lines = textwrap.wrap(text, width, break_long_words=False)






如果您想自己编写代码,这就是我的处理方式:首先,将文本拆分为单词。从一行中的第一个单词开始,然后迭代其余单词。如果下一个单词适合当前行,请添加它,否则请完成当前行并将该单词用作下一行的第一个单词。重复直到所有单词都用完。


If you want to code it yourself, this is how I would approach it: First, split the text into words. Start with the first word in a line and iterate the remaining words. If the next word fits on the current line, add it, otherwise finish the current line and use the word as the first word for the next line. Repeat until all the words are used up.

以下是一些代码:

text = "hello, this is some text to break up, with some reeeeeeeeeaaaaaaally long words."
n = 16

words = iter(text.split())
lines, current = [], next(words)
for word in words:
    if len(current) + 1 + len(word) > n:
        lines.append(current)
        current = word
    else:
        current += " " + word
lines.append(current)

这篇关于将字符串拆分为最大长度为X的片段-仅在空格处拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆