每64个ish字符插入一个换行符



有人能给我指出正确的方向吗。。

我有一个包含单词句子的字符串例如,"他试图学习一种解决问题的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第1行:人工智能(AI(,有时也称为机器智能,第2行:智能是由机器展示的,第3行:与显示的自然智能形成对比人类和其他动物。第4行:在计算机科学中,人工智能研究被定义为

但textwrap给了我这个:

123456789 123456789第1行:人工智能(AI(,有时也称为机器智能,第2行:智能得到证明了吗通过机器,3号线:与自然形成对比人类和其他动物表现出的智力。第4行:在计算机科学中,人工智能研究被定义为

我怀疑Regex可能是答案,但我无法用Regex来解决这个问题。

在换行符上将长字符串拆分为单独的行。"像往常一样"将每一行单独换行,然后将所有内容再次连接到一个字符串中。

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返回一个字符串列表,您不需要做任何其他事情,只需要继续将它们粘贴在一起,并在最后将它们连接起来。

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)]

每个字符串的行上迭代

如果您能提供您已经尝试过的任何代码,它可能会帮助我们回答您的问题。话虽如此,我相信下面的示例代码将保留现有的换行符,换行超过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.

最新更新