每64个字符插入换行符

2024-05-23 19:16:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我有一个包含单词句子的字符串 e、 g.“他试图学习一种pythonic,或者regex,解决问题的方法”

这个字符串非常大,我需要把它分成多行,每行不能超过64个字符。 但我不能每64个字符插入一个换行符。我需要确保换行符出现在第64个字符之前最接近的字符(来自一组字符)处,以确保行不超过64个字符。 e、 我只能在空格、逗号或句号后插入换行符

我还需要解决方案是相当有效的,因为这是一个行动,将发生很多次。你知道吗

使用textwrap

我不确定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

我怀疑Regex可能是答案,但我在试图用Regex解决这个问题时已经超出了我的深度。你知道吗


Tags: 字符串byislinemachineailongintelligence
3条回答

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

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

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

相关问题 更多 >