简体   繁体   English

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

[英]How to write function that takes in a string message and returns a list of string messages with pagination

I want to write a function that takes in a string message and returns a list of string messages with pagination if needed. 我想编写一个接受字符串消息并在需要时返回带有分页的字符串消息列表的函数。

This is what I have 这就是我所拥有的

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()

Input Text/String: 输入文字/字串:

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.

My Output: 我的输出:

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.

Expected Results 预期成绩

As an example, the following message has 212 characters: 例如,以下消息具有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.

The message should be sent in two chunks as such: 消息应按以下两个块发送:

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)

and

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

You can't use textwrap.fill() here for your purposes: 您不能在此处使用textwrap.fill()来达到目的:

Wraps the single paragraph in text, and returns a single string containing the wrapped paragraph. 将单个段落包装为文本,并返回包含被包装段落的单个字符串。

Instead, you need to use textwrap.wrap() 相反,您需要使用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. 返回输出行列表,不带最终换行符。

Since it already returns a list, there isn't much left for you to do here. 由于它已经返回了列表,因此您在这里没有什么可做的了。

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())

Now that you have a list of strings that are width=100 characters long, you can now do whatever it is you want with your strings. 现在您有了一个width=100字符长的字符串列表,现在您可以对字符串进行任何操作。 For example, if you wanted to print them you would do: 例如,如果要打印它们,则可以执行以下操作:

for each in smart_collect():
    print(each)

Now if you wanted to add pagination, you could do this: 现在,如果您想添加分页,则可以执行以下操作:

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})')

For your input, your result (with width=100) looks like this: 对于您的输入,结果(宽度为100)如下所示:

As a reminder, you have an appointment with Dr. Smith tomorrow at 3:30 pm. 提醒一下,您明天下午3:30与Smith博士约好。 If you are unable to make (1/3) 如果您无法做出(1/3)
this appointment, please call our customer service line at least 1 hour before your scheduled (2/3) 预约后,请至少在预定时间(2/3)前1小时致电我们的客户服务热线
appointment time. 预约时间。 (3/3) (3/3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:带列表和分隔符并返回字符串的函数 - Python: Function that takes a list and a separator and returns a string 您如何在Python中编写一个函数,该函数需要一个字符串并返回一个新字符串,该字符串是原始字符串,并且重复了所有字符? - How do you write a function in Python that takes a string and returns a new string that is the original string with all of the characters repeated? 我想编写一个函数,该函数接受一个列表并返回所有已删除的重复项,而不创建另一个列表或字符串 - I want to write a function that takes a list and returns it with all duplicates removed, without creating another list or string 如何编写一个接受字符串并返回该字符串中的第一个单词的函数 - How do I Write a function that takes in a string and returns the first word in that string 编写一个名为 justify 的 function,它采用字符串列表和文件大小,然后返回单个字符串 - Write a function called justify that takes a list of strings and a filed size then returns a single string 写一个 function 需要 2 个字符串 arguments - write a function that takes 2 string arguments 获取字符串并返回8个字符的字符串列表的函数 - Function that takes a string and returns a list of 8-character strings Stemmer function 接受一个字符串并返回列表中每个单词的词干 - Stemmer function that takes a string and returns the stems of each word in a list 编写一个函数,该函数接受一个字符串参数并返回一个新字符串,其中包含所有不在参数字符串中的字母表字母 - Write a function that takes a string parameter and returns a new string with all the letters of the alphabet that are not in the argument string 我如何在 python 中编写一个 function 作为输入 2 个数字和 1 个字符串 - How do I write a function in python that takes as input 2 numbers, and 1 string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM