python - 如何编写接收字符串消息并返回带分页的字符串消息列表的函数

标签 python python-3.x

我想编写一个函数,它接受一个字符串消息,并在需要时返回一个带有分页的字符串消息列表。

这是我的

import textwrap

def smart_collect():
    text = input('Please Enter a text: ')
    dedented_text = textwrap.dedent(text).strip()
    wrap_text = textwrap.wrap(dedented_text, width=212)
    max_page = len(wrap_text)

    for i in range(0, max_page):
        print(f'{wrap_text[i]} ({i+1}/{max_page})')


smart_collect()

输入文本/字符串:

As a reminder, you have an appointment with Dr. Smith tomorrow at 
3:30 pm. If you are unable to make this appointment, please call our 
customer service line at least 1 hour before your scheduled 
appointment time.

我的输出:

As a reminder, you have an appointment with Dr. Smith tomorrow at(1/1)
3:30 pm. If you are unable to make this appointment, please call our 
3:30: command not found
customer service line at least 1 hour before your scheduled customer: 
command not found
appointment time.

预期结果

例如,以下消息有 212 个字符:

As a reminder, you have an appointment with Dr. Smith tomorrow at 
3:30 pm. If you are unable to make this appointment, please call our 
customer service line at least 1 hour before your scheduled 
appointment time.

消息应该分两部分发送:

As a reminder, you have an appointment with Dr. Smith tomorrow at 
3:30 pm. If you are unable to make this appointment, please call our 
customer service  (1/2)

line at least 1 hour before your scheduled appointment time. (2/2)

最佳答案

您不能出于您的目的在此处使用 textwrap.fill():

Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph.

相反,您需要使用 textwrap.wrap()

Wraps the single paragraph in text (a string) so every line is at most width characters long. Returns a list of output lines, without final newlines.

因为它已经返回了一个列表,所以您在这里没有什么可做的了。

def smart_collect():
    text = input('Please Enter a text: ')
    dedented_text = textwrap.dedent(text).strip()
    wrap_text = textwrap.wrap(dedented_text, width=100)
    return wrap_text

print(smart_collect())

现在您已经有了一个长度为 width=100 字符的字符串列表,您现在可以对字符串做任何您想做的事情了。例如,如果你想打印它们,你会这样做:

for each in smart_collect():
    print(each)

现在如果你想添加分页,你可以这样做:

list_strings = smart_collect()
max_page = len(list_strings)
for i in range(0, max_page):
    print(f'{list_strings[i]} ({i+1}/{max_page})')

对于您的输入,您的结果(width=100)如下所示:

As a reminder, you have an appointment with Dr. Smith tomorrow at 3:30 pm. If you are unable to make (1/3)
this appointment, please call our customer service line at least 1 hour before your scheduled (2/3)
appointment time. (3/3)

关于python - 如何编写接收字符串消息并返回带分页的字符串消息列表的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55737382/

相关文章:

python - 选择随机 Action 的 Tensorflow 代理

python - 如何让 on_press 在自定义按钮中工作?

arrays - Numpy 4x3 矩阵 LinAlgError : 1-dimensional array given. 数组必须至少是二维的

python - 任何内置的工作 `range(len(lst))` ?

python - 如何在 Python 3.x 中强制输入整数?

python - 从一个数据框中删除另一个数据框中存在的行

python - 如何使用单个反斜杠转义字符串的特殊字符

Python 网络/cidr 计算

python - 如何在excel中加粗csv数据?

Python ssl 标准库 load_cert_chain - 加载 PEM 证书链失败