【问题标题】:Python - I need a specific way of making multi-line input [duplicate]Python - 我需要一种进行多行输入的特定方法[重复]
【发布时间】:2015-02-08 15:41:47
【问题描述】:

我需要在 Python 中进行多行输入。 这是我的程序的一部分:

input_ = input("Enter text:\n") #This can handle only single line of text

像这样:

Enter text:

This is frst line, #Variable input_ would be only this line
this is second line,
this is last line.

我需要一些要求用户输入文本的东西(多行文本) 当用户粘贴文本(Ctrl + C)并按Enter键时,程序 需要将整个文本放入列表中。

附:请不要将此标记为重复,因为它与其他对我没有帮助的问题有点不同。

【问题讨论】:

  • 如何知道输入何时结束?
  • 当用户按下 Enter 时输入结束。
  • 在多行输入中,每行后面都有一个换行符(回车)。您如何将其与“用户输入的输入结束输入”区分开来?
  • 用户需要粘贴文本,一旦按下输入就结束了。
  • 在安特下面看我的回答。用户可以粘贴然后回车。

标签: python text input line break


【解决方案1】:

尝试使用 while 循环:

listed = []
inputs = None

while inputs != '':
    inputs = input()
    listed.append(inputs)

listed = [i for i in listed if i != '']
print listed

【讨论】:

  • 如果我输入“某段\n\n某段”怎么办?
  • 它应该可以正常工作,直到输入为空。
  • 但是用户需要在输入后按两次回车,我能以某种方式解决这个问题吗?
  • 不,这是处理多行输入的最接近的方法。
  • 好的,您可以编辑将文本保存到列表中的代码吗(1 行 = 1 个列表项)
猜你喜欢
  • 2021-05-18
  • 2019-05-15
  • 2016-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
  • 1970-01-01
相关资源
最近更新 更多