在Python中,有没有一种很好的方法可以在不拆分单词的情况下拆分(可能)长字符串?

在Python中,有没有一种很好的方法可以在不拆分单词的情况下拆分(可能)长字符串?,python,string,split,word,word-wrap,Python,String,Split,Word,Word Wrap,我想确保我只打印最多80个字符的行,但我有一个字符串s,它可以比这短也可以长。所以我想把它分成几行,不分任何单词 长字符串示例: s = "This is a long string that is holding more than 80 characters and thus should be split into several lines. That is if everything is working properly and nicely and all that. No mis

我想确保我只打印最多80个字符的行,但我有一个字符串
s
,它可以比这短也可以长。所以我想把它分成几行,不分任何单词

长字符串示例:

s = "This is a long string that is holding more than 80 characters and thus should be split into several lines. That is if everything is working properly and nicely and all that. No mishaps no typos. No bugs. But I want the code too look good too. That's the problem!"
我可以想出这样做的方法,例如:

words = s.split(" ")
line = ""
for w in words:
    if len(line) + len(w) <= 80:
        line += "%s " % w
    else:
        print line
        line ="%s " % w

print line

所有这些都不是很优雅,所以我的问题是,是否有一种更酷的蟒蛇式的方式来做到这一点?(可能与regex等一起使用。)

有一个模块:

例如,您可以使用

print '\n'.join(textwrap.wrap(s, 80))


这有一个模块:

例如,您可以使用

print '\n'.join(textwrap.wrap(s, 80))

输出:

This is a long string that is holding more than 80 characters and thus should be
split into several lines. That is if everything is working properly and nicely
and all that. No misshaps no typos. No bugs. But I want the code too look good
too. That's the problem!
输出:

This is a long string that is holding more than 80 characters and thus should be
split into several lines. That is if everything is working properly and nicely
and all that. No misshaps no typos. No bugs. But I want the code too look good
too. That's the problem!

您可以试试这个python脚本

import os, sys, re
s = "This is a long string that is holding more than 80 characters and thus should be split into several lines. That is if everything is working properly and nicely and all that. No misshaps no typos. No bugs. But I want the code too look good too. That's the problem!"
limit = 83
n = int(len(s)/limit)
b = 0
j= 0
for i in range(n+2):

    while 1:
        if s[limit - j] not in [" ","\t"]:
            j = j+1
        else:
            limit = limit - j
            break
    st = s[b:i*limit]
    print st
    b = i*limit

您可以试试这个python脚本

import os, sys, re
s = "This is a long string that is holding more than 80 characters and thus should be split into several lines. That is if everything is working properly and nicely and all that. No misshaps no typos. No bugs. But I want the code too look good too. That's the problem!"
limit = 83
n = int(len(s)/limit)
b = 0
j= 0
for i in range(n+2):

    while 1:
        if s[limit - j] not in [" ","\t"]:
            j = j+1
        else:
            limit = limit - j
            break
    st = s[b:i*limit]
    print st
    b = i*limit

与基本的换字算法相比,这是一个糟糕的笑话,而不是速度。刚刚将其与textwrap进行了对比,速度大约快了50倍。(注意,我知道速度不是一切,只是有趣而已)速度是(几乎-你仍然可以尝试更改需求)如果功能缺失,什么都不是;)与基本的换字算法相比,这是一个糟糕的笑话,而不是速度。刚刚将其与textwrap进行了对比,速度大约快了50倍。(注意,我知道速度不是一切,只是有趣而已)速度是(几乎-你仍然可以尝试更改需求)如果功能缺失,什么都不是;)你的问题和我几天前问的问题很相似。我错过了那个搜索旧帖子的帖子,我猜是因为我在讨论包装时寻找拆分,但它们是相似的。好吧,从技术上讲,这叫做包装。你的问题看起来和我几天前问的问题相似。我错过了那篇搜索旧帖子的文章,我猜是因为我在讨论包装时寻找拆分,但是的,它们是相似的。从技术上讲,这就是包装。
import os, sys, re
s = "This is a long string that is holding more than 80 characters and thus should be split into several lines. That is if everything is working properly and nicely and all that. No misshaps no typos. No bugs. But I want the code too look good too. That's the problem!"
limit = 83
n = int(len(s)/limit)
b = 0
j= 0
for i in range(n+2):

    while 1:
        if s[limit - j] not in [" ","\t"]:
            j = j+1
        else:
            limit = limit - j
            break
    st = s[b:i*limit]
    print st
    b = i*limit