输出字符串看起来像Python中的报纸文章

时间:2018-10-17 18:00:25

标签: python string python-3.x output

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

我已经实现了这一目标。。但是,我希望实现一个系统,使中间的单词不会被切断,从而影响阅读的流畅度。相反,我希望在行的eand处插入一个连字符。

这是我当前的输出:

Picture link

这是我希望获得的输出:

Picture link

我尝试更改第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)

3 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:0)

即使这是直接答案,我还是通过对您的代码进行一些更正来扩展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)

答案 2 :(得分:0)

要使其看起来像报纸,应使用适当的连字算法。 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个字符。连字符仅用于长字,并遵循特定于语言的连字符规则。

Contrary to popular
belief, Lorem Ipsum
is not simply random
text. It has roots
in a piece of clas-
sical Latin litera-
ture from 45 BC,
making it over 2000
years old.