Python 3:如何在使用input()时忽略换行符
对于上面的代码,当提示提供输入时,如果粘贴具有多个换行符的文本。代码将针对换行次数运行!我只希望它在整个文本中运行一次Python 3:如何在使用input()时忽略换行符,python,python-3.x,input,file-io,line-breaks,Python,Python 3.x,Input,File Io,Line Breaks,对于上面的代码,当提示提供输入时,如果粘贴具有多个换行符的文本。代码将针对换行次数运行!我只希望它在整个文本中运行一次 有人能帮忙吗?您可以使用sys.stdin.read(),但需要手动发送EOT字符: while count != 5: input_text = input("Please insert a number of lines of text \n") if count != 5: print("Count is " + str(count)) 请注意,在粘
有人能帮忙吗?您可以使用
sys.stdin.read()
,但需要手动发送EOT字符:
while count != 5:
input_text = input("Please insert a number of lines of text \n")
if count != 5:
print("Count is " + str(count))
请注意,在粘贴后的最后,我使用Enter键,然后使用ctrl-D键。我没有找到您问题的确切答案,但是我注意到,当您在shell中复制多行文本时,它只会将第一行文本指定给输入文本,然后再次运行并将第二行指定给输入文本,再次运行,输入文本的第三行。你看 我认为input语句不适合多行,尽管您可以找到一些解决方法 下面的代码显示了每次运行循环时,变量如何在您复制到shell中的下一行发生变化:
>>> 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
>>>
count = 0
while True:
count += 1
input_text = input("Please insert a number of lines of text \n")
print("Count is " + str(count))
print("what the variable is set to first time its running the loop: ",input_text,"\n")