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

2024-03-29 14:05:46 发布

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

我试图在控制台中输出一个字符串,这样,在一个固定的行长度上,它会在一个新行上切断并继续该字符串,从而使该字符串看起来像一篇报纸文章。你知道吗

这一点,我已经做到了。然而,我希望实现一个系统,这样字就不会在中间被切断,扰乱阅读的流程。相反,我希望在行的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)

Tags: ofthe字符串代码inifis字符
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)

为了使它看起来像报纸,你应该使用一个适当的连字算法。 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.

您的代码在每一行的末尾都放置了一个破折号,因为每一个(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

这将需要更多的处理。。。如果你的努力值得的话。你知道吗

相关问题 更多 >