每隔64个字符插入换行符

时间:2018-11-06 00:08:32

标签: python regex string python-3.x

有人可以为此指出正确的方向吗。

我有一个包含单词句子的字符串 例如“他正试图学习一种解决问题的Python或正则表达式的方法”

有问题的字符串很大,我需要将其分成多行,每行不能超过64个字符。 但是我不能每64个字符插入一个换行符。我需要确保该中断发生在第64个字符之前的最近字符处(从一组字符开始),以确保该行不超过64个字符。 例如我只能在空格,逗号或句号后插入换行符

我还需要解决方案非常有效,因为它会多次执行。

使用自动换行

我不确定textwrap是解决我的问题的方法,因为我需要保留输入字符串中的原始换行符。 示例:

long_str = """
123456789 123456789 123456789 123456789 123456789 123456789
Line 1: Artificial intelligence (AI), sometimes called machine intelligence, 
Line 2: is intelligence demonstrated by machines, 
Line 3: in contrast to the natural intelligence displayed by humans and  other animals. 
Line 4: In computer science AI research is defined as
"""
lines = textwrap.wrap(long_str, 60, break_long_words=False)
print('\n'.join(lines))

我想要的是这个

123456789 123456789 123456789 123456789 123456789 123456789
Line 1: Artificial intelligence (AI), sometimes called 
machine intelligence, 
Line 2: is intelligence demonstrated by machines, 
Line 3: in contrast to the natural intelligence displayed 
by humans and other animals. 
Line 4: In computer science AI research is defined as

但是textwrap给了我这个:

 123456789 123456789 123456789 123456789 123456789 123456789
Line 1: Artificial intelligence (AI), sometimes called
machine intelligence,  Line 2: is intelligence demonstrated
by machines,  Line 3: in contrast to the natural
intelligence displayed by humans and other animals.  Line 4:
In computer science AI research is defined as

我怀疑正则表达式可能是答案,但是我不愿意使用正则表达式来解决这个问题。

3 个答案:

答案 0 :(得分:1)

import textwrap

def f1(foo): 
    return iter(foo.splitlines())

long_str = """
123456789 123456789 123456789 123456789 123456789 123456789
Line 1: Artificial intelligence (AI), sometimes called machine intelligence, 
Line 2: is intelligence demonstrated by machines, 
Line 3: in contrast to the natural intelligence displayed by humans and  other animals. 
Line 4: In computer science AI research is defined as
"""
[print('\n'.join(textwrap.wrap(l, 64, break_long_words=False))) for l in f1(long_str)]

,每个this

都在字符串的行上进行迭代

答案 1 :(得分:1)

将长字符串拆分为换行符。像往常一样将每一行换行,然后将所有内容再次连接为一个字符串。

import textwrap

long_str = """
123456789 123456789 123456789 123456789 123456789 123456789
Line 1: Artificial intelligence (AI), sometimes called machine intelligence, 
Line 2: is intelligence demonstrated by machines, 
Line 3: in contrast to the natural intelligence displayed by humans and  other animals. 
Line 4: In computer science AI research is defined as
"""

lines = []
for line in long_str.split('\n'):
    lines += textwrap.wrap(line, 60, break_long_words=False)
print('\n'.join(lines))

textwrap返回一个字符串列表,除了继续将它们粘贴在一起并在最后加入它们之外,您无需执行其他任何操作。

答案 2 :(得分:0)

如果您可以提供您已经尝试过的任何代码,它可能会帮助我们回答您的问题。话虽如此,我相信以下示例代码将保留现有的换行符,包装超过64个字符的行,并保留其余字符串的格式。

import textwrap

long_str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, " \
       "sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. " \
       "Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris" \
       "nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in" \
       "reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. " \
       "Excepteur sint occaecat cupidatat non proident, sunt in culpa qui" \
       "officia deserunt mollit anim id est laborum."

lines = textwrap.wrap(long_str, 64, break_long_words=False)

print('\n'.join(lines))

Python的输出是:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut
enim ad minim veniam, quis nostrud exercitation ullamco
laborisnisi ut aliquip ex ea commodo consequat. Duis aute irure
dolor inreprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa quiofficia deserunt mollit anim id est
laborum.