【问题标题】:Inserting newline characters every 64-ish characters每 64 个字符插入换行符
【发布时间】:2019-04-09 09:13:21
【问题描述】:

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

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

但是 textwrap 给了我这个:

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

我怀疑正则表达式可能是答案,但我无法用正则表达式解决这个问题。

【问题讨论】:

  • 我看了这个,这几乎是我所追求的,但它似乎并没有保留现有的换行符。我只需要换行超过 64 个字符的行/句子,并且我需要保留字符串其余部分的格式
  • 为了一个简单的解决方案,将它分成几行并在它们上调用 textwrap。

标签: python regex string python-3.x


【解决方案1】:

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

【讨论】:

  • 谢谢@Andy。但我不确定 textwrap 会解决这个问题。我已根据您的输入更新了我的 OP
  • 那不是保留原来的换行符。
【解决方案2】:
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的字符串行迭代

【讨论】:

  • 这与 usr2564301 的建议相比如何?
  • @CHaste 几乎相同(列表理解而不是 for)。但是,我的帖子是几秒钟前发布的。 ;)
【解决方案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 返回一个字符串列表,您不需要做任何其他事情,只需继续将它们粘贴在一起,并在最后加入它们。

【讨论】:

  • 谢谢。我曾考虑过这样做,但我确信必须有更好的方法。如果我添加 drop_whitespace=False 那么它给了我我需要的东西
  • 我会接受这个作为答案,因为对于我这个 Python 新手来说,与@DonQuiKong 提出的解决方案相比,这个解决方案的可读性更好
  • @CHaste 这样做,这个答案也解释了一点。顺便说一句,列表推导“更符合 Python 风格”并且速度更快。
猜你喜欢
  • 2011-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多