简体   繁体   English

如何禁用在 Python 3 中触发输入的回车键?

[英]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.

相关问题 如果循环中未提供输入(或按下回车键),如何在 python 中结束无限循环 - How do I end an infinite loop in python if no input(or the enter key is pressed) is provided in the loop 使用 Python 中的 Enter 键触发按钮单击 - Triggering a button click with the enter key in Python Python-如何在绝对没有输入的情况下继续循环,包括输入键 - Python - how to continue a loop with absolutely no input, including enter key stroke 如何重置我的计算器,当我输入“$”而不是数字时,输入 python - How do I reset my calculator, when I enter "$" to input , instead of a number , python 在python中按下键盘上的“ enter”键时,如何设置特定的动作 - How do I set a specific action to happen when the “enter” key on my keyboard is pressed in python 如何使用 python selenium chrome webdriver 按 shift + enter 键? - How Do I press shift + enter key using python selenium chrome webdriver? 如何在 python 中输入固定的用户名和密码而不是要求输入字段? - How do I enter a fixed username and password rather than requiring an input field in python? 如何在输入按键之前将通过键盘输入的文本打印到 python 的 input() 方法? - How can I print the text which was being entered through keybord to the input() method of python before enter key press? 如何使用 Tkinter 中的 Enter 键激活按钮单击? - How do I activate button click with the Enter key in Tkinter? 如何将回车键绑定到 tkinter 中的 function? - How do I bind the enter key to a function in tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM