【问题标题】:Output string to look like newspaper article in Python输出字符串看起来像 Python 中的报纸文章
【发布时间】:2018-10-17 18:00:24
【问题描述】:

我正在尝试在控制台中输出一个字符串,以便以固定的行长度切断该字符串并在新行上继续该字符串 - 使该字符串具有报纸文章的外观。

这个,我做到了。 但是,我希望实现一个系统,使单词不会在中间被切断,打乱阅读的流程。我希望在行尾插入一个连字符。

这是我目前的输出:

这是我想要的输出:

我尝试在第 9 行更改我的代码,使其如下所示,但这导致连字符被放置在每一行的末尾——这不是我想要的。

if(outStr[i+1] == " "):

如何更改我的代码以创建我想要的输出?此函数必须能够处理任何长字符串,因为它将作为更大程序的一部分多次使用。

这是我用 Python 3.6.5 编写的代码:

lorumIpsum = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source."

def OutputStringToBook(outStr):
    # Take each character
    for i in range(len(outStr)):
        # If the character is at the designated line end
        if (i % 30 == 0):
            # If the next character is not a space
            if(outStr[i+1] != " "):
                print()
            else:
                print("-") 
            print(outStr[i], end="")
        else:
            print(outStr[i], end="")
    #time.sleep(0.01)
OutputStringToBook(lorumIpsum)

【问题讨论】:

  • range(len(outStr)) 在 Python 中是一个很大的禁忌(大部分情况下)。请改用for ind, i in enumerate(outStr)
  • @Agile_Eagle:该链接不适用。 OP 能够很好地分配字符串;问题是自动换行。

标签: python string python-3.x output


【解决方案1】:

您的代码在每一行的末尾放置一个破折号,因为碰巧每 (30n+1) 个字符都是非空白字符。首先,当两个跨行字符不是空白时,您只想 使用连字符:

if(outStr[i] == " " or outStr[i+1] == " "):

现在,这仍然存在以下问题:您插入连字符而不考虑实际的音节划分,并且您没有在边缘删除空格:

Contrary to popular belief, Lo-
rem Ipsum is not simply random
 text. It has roots in a piece
 of classical Latin literature
 from 45 BC, making it over 20-
00 years old. Richard McClinto-
ck, a Latin professor at Hampd-
en-Sydney College in Virginia,
 looked up one of the more obs-
cure Latin words, consectetur,
 from a Lorem Ipsum passage, a-
nd going through the cites of -
the word in classical literatu-
re, discovered the undoubtable

如果你想删除边缘空格,你需要做更多的工作:不打印空格会抛出你的位置计数,这表明你需要使用一个每个消耗 30 或 31 个字符的字符串行(取决于删除前导空格)。做出“智能”连字符选择需要一个连字符字典(是的,有这样的东西)和更多的处理。另外,适应一行可用的字符少于 30 个需要在行内插入空格,例如更改

rem Ipsum is not simply random
text. It has roots in a piece

rem Ipsum is not simply random
text.  It has roots in a piece

这将需要更多的处理......如果值得为您的需要付出努力。

【讨论】:

    【解决方案2】:

    要使其看起来像报纸,您应该使用适当的断字算法。 PyHyphen 库包含 libreoffice 中使用的连字符字典,并支持多种语言(默认语言为 en_US)。

    # pip install pyhyphen
    from hyphen import Hyphenator
    from textwrap2 import wrap
    english = Hyphenator('en_US')
    print('\n'.join(wrap(lorem_text, width=20, use_hyphenator=english)))
    

    输出将如下所示。请注意,有些行少于 20 个字符。连字符仅用于长词,并遵循语言特定的连字符规则。

    与流行相反 信念,Lorem Ipsum 不仅仅是随机的 文本。它有根 在一个班级中 古拉丁文 从公元前 45 年开始, 超过2000 岁。

    【讨论】:

      【解决方案3】:

      尽管这是一个直接的答案,但我正在扩展 Prune 的答案,并对您的代码进行一些更正:

      我们的想法是删除不必要的印刷并使其更清晰。

      lorumIpsum = "Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source."
      def OutputStringToBook(outStr):
          # Take each character
          for i, item in enumerate(outStr):
              # If the character is at the designated line end
              if (i % 30 == 0):
                  # If the current or next character is not a space
                  if(item == " " or outStr[i+1] == " "):
                      print("-", end='')
                  print()
              print(item, end='')
      
      OutputStringToBook(lorumIpsum)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-04
        • 1970-01-01
        • 2018-11-15
        • 1970-01-01
        • 2020-02-29
        • 1970-01-01
        • 2010-12-20
        • 1970-01-01
        相关资源
        最近更新 更多