每 n 个字符插入一个换行符,使用 Python 在换行符处重置 n
Insert a line-break every n characters, reset n on line-break using Python
我从 Jira REST-Api 获得了一段文本。我需要每 150 个字符插入一个换行符。
如果第 150 个字符不是空格,则将换行符插入最后一个空格,如果该文本包含换行符,则应重置该计数。
我已经用正则表达式试过了,但是 deletes/ignores 文本中已经换行了,它会在单词中间插入换行符
featureText = re.sub("(.{150})", "\1\n", featureText, 0, re.DOTALL)
#featureText contains some text from the api get request
为简单起见,假设我想每 10 个字符添加一个换行符。我有文字
My Television is broken
and that sucks
我目前得到
My Televis
ion is bro
ken and th
at sucks
我想要的是
My
Television
is broken
and that
sucks
编辑:澄清了我的问题,它是现实世界的失败者。
只有示例使用了 10 个字符,我的实际问题使用了 150 个字符所以不用担心将一个单词切成两半,我想不会有任何单词长 150 个字符。
类似于:
def split_text(text, n):
for line in text.splitlines():
while len(line) > n:
x, line = line[:n], line[n:]
yield x
yield line
text = "abcdefghijklmnopqr\nstuvwxyz"
for line in split_text(text, 10):
print(line)
打印:
abcdefghij
klmnopqr
stuvwxyz
我会像这样使用 textwrap:
import textwrap
example = textwrap.dedent("""\
My Television is broken
and that sucks""")
print '\n'.join(l for line in example.splitlines()
for l in textwrap.wrap(line, width=10))
这导致:
My
Television
is broken
and that
sucks
一个更好的例子是:
example = textwrap.dedent("""\
My Television is
and that sucks""")
这导致:
My
Television
is
and that
sucks
这更能说明原来的线是单独包装的。
我从 Jira REST-Api 获得了一段文本。我需要每 150 个字符插入一个换行符。
如果第 150 个字符不是空格,则将换行符插入最后一个空格,如果该文本包含换行符,则应重置该计数。
我已经用正则表达式试过了,但是 deletes/ignores 文本中已经换行了,它会在单词中间插入换行符
featureText = re.sub("(.{150})", "\1\n", featureText, 0, re.DOTALL)
#featureText contains some text from the api get request
为简单起见,假设我想每 10 个字符添加一个换行符。我有文字
My Television is broken
and that sucks
我目前得到
My Televis
ion is bro
ken and th
at sucks
我想要的是
My
Television
is broken
and that
sucks
编辑:澄清了我的问题,它是现实世界的失败者。 只有示例使用了 10 个字符,我的实际问题使用了 150 个字符所以不用担心将一个单词切成两半,我想不会有任何单词长 150 个字符。
类似于:
def split_text(text, n):
for line in text.splitlines():
while len(line) > n:
x, line = line[:n], line[n:]
yield x
yield line
text = "abcdefghijklmnopqr\nstuvwxyz"
for line in split_text(text, 10):
print(line)
打印:
abcdefghij
klmnopqr
stuvwxyz
我会像这样使用 textwrap:
import textwrap
example = textwrap.dedent("""\
My Television is broken
and that sucks""")
print '\n'.join(l for line in example.splitlines()
for l in textwrap.wrap(line, width=10))
这导致:
My
Television
is broken
and that
sucks
一个更好的例子是:
example = textwrap.dedent("""\
My Television is
and that sucks""")
这导致:
My
Television
is
and that
sucks
这更能说明原来的线是单独包装的。