[英]How do I disable the enter key triggering an input in Python 3?
So I'm trying to make a simple word counting program, and I want it to be able to take enter keys as part of the essay it's reading without being triggered.所以我正在尝试制作一个简单的单词计数程序,我希望它能够将输入键作为它正在阅读的文章的一部分而不会被触发。 (If anyone didn't understand, if I copy paste a paragraph that has the enter character in it, python will simply stop receiving the string at the enter char, because it thinks the user pressed enter, and it will take an incomplete input).
(如果有人不明白,如果我复制粘贴一个包含回车字符的段落,python 将停止接收回车字符处的字符串,因为它认为用户按下回车,并且输入不完整) . I don't think I can explain this too well, so try copy pasting this into the code:
我认为我不能很好地解释这一点,因此请尝试将其复制粘贴到代码中:
Line 1: My name is Jack.
第 1 行:
My name is Jack.
Line 2: Jack is my name.
第 2 行:
Jack is my name.
It only accepts line one.它只接受第一行。 Is there a workaround to this problem?
这个问题有解决方法吗? Or is there a completely different approach I should take?
还是我应该采取完全不同的方法?
Code:代码:
#Count words in an essay
import time
SA = input('Copy paste essay: \n')
word_list = SA.split(' ')
print (str(len(word_list)) + ' is the number of words in your essay')
In python 3.x, you can use sys.stdin.read()
to read multiline input on the console.在 python 3.x 中,您可以使用
sys.stdin.read()
读取控制台上的多行输入。
>>> import sys
>>> text = sys.stdin.read()
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
^Z
>>> print(text)
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
>>>
To exit the input mode, you have to use Ctrl
+ Z
which appears in the above shell snippet as ^Z
.要退出输入模式,您必须使用
Ctrl
+ Z
出现在上面的 shell 片段中作为^Z
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.