Python 3:使用input()时如何忽略换行符 [英] Python 3: How to ignore line breaks when using input()

查看:161
本文介绍了Python 3:使用input()时如何忽略换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

while count != 5:

  input_text = input("Please insert a number of lines of text \n")

  if count != 5:
    print("Count is " + str(count))

对于上面的代码,如果提示我提供输入,请粘贴带有多个换行符的文本.该代码将以换行符的数量运行!我只希望它对整个文本运行一次.

For the code above, when prompted to supply input, if I paste in a text with multiple line breaks. The code will run for the number of line breaks! I just want it to run ONCE for the entire text.

任何人都可以帮忙吗?

推荐答案

您可以使用 sys.stdin.read(),但需要您手动发送EOT字符:

You can use sys.stdin.read() but it will require you to manually send the EOT character:

>>> import sys
>>> x = sys.stdin.read()
the quick brown fox
jumped over the lazy
dog
>>> print(x)
the quick brown fox
jumped over the lazy
dog

>>>

注意,粘贴后,我先使用 Enter ,然后使用 ctrl-D .

Notice, at the end after I pasted I use Enter and then ctrl-D.

这篇关于Python 3:使用input()时如何忽略换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆