简体   繁体   English

从sys.stdin读取,用RETURN结束用户输入

[英]Reading from sys.stdin, ending user input with RETURN

I am working on a script with user input in Py 3.6. 我正在使用Py 3.6中的用户输入编写脚本。

In the script, the user is asked to enter a text section - potentially containing new lines - into the shell. 在脚本中,要求用户在shell中输入文本部分(可能包含新行)。 The entered text will then be saved to a Python variable for further processing. 然后输入的文本将保存到Python变量中以供进一步处理。

Since the user input may contain newlines, I think I cannot use input() but am using sys.stdin.read() (as suggested here ). 由于用户输入可能包含换行符,我想我不能使用input()但是使用sys.stdin.read() (如此处所示 )。

Problem 问题

Reading in the input works fine, but to end the user input, the user has to hit Return and then use the key combination CTRL + d (see here ). 读入输入工作正常,但要结束用户输入,用户必须按Return键然后使用组合键CTRL + d (参见此处 )。 (See Current Procedure below) (见下面的当前程序

Question

  • I would prefer that the user can just end their input to sys.stdin.read by hitting the Return key (cf Expected Procedure below) 我希望用户可以通过点击Return键结束他们对sys.stdin.read的输入(参见下面的预期程序

EDIT: Any other simplification to the current process with CTRL + d is appreciated as well. 编辑:使用CTRL + d对当前进程的任何其他简化也是值得赞赏的。

  • Is this doable? 这可行吗?

  • There are some hacks here but I thought maybe there is a better way 这里有一些黑客但我想也许有更好的方法

Current code 目前的代码

    # display text on screen
    print("Review this text\n" + text)
    # user will copy and paste relevant items from text displayed into Terminal
    user_input =  sys.stdin.read() 
    print ("hit ctrl + d to continue")
    # process `user_input`

Current procedure 目前的程序

With the current code reproduced below, the user has to 使用下面转载的当前代码,用户必须

1) paste the text 2) hit RETURN to end input 3) hit Ctrl+d to move to next file 1)粘贴文本2)按RETURN键结束输入3) Ctrl+d移动到下一个文件

Expected procedure 预期程序

I would like to reduce this to: 我想减少这个:

1) paste the text 2) hit RETURN to end input and move to next file 1)粘贴文本2)按RETURN键结束输入并移动到下一个文件

Running Python 3.5.6 on MacOSX, using Terminal for text input. 在MacOSX上运行Python 3.5.6,使用Terminal进行文本输入。 Any help is much appreciated! 任何帮助深表感谢!

Based on your reply in the comment, if terminating by empty line is acceptable (ie your input text cannot contain newline on its own except to terminate input), that is quote trivial: 根据您在评论中的回复,如果以空行终止是可以接受的(即您的输入文本除了终止输入外不能自己包含换行符),这是一个简单的引用:

user_input = ''          # User input we'll be adding to
for line in sys.stdin:   # Iterator loops over readline() for file-like object
    if line == '\n':     # Got a line that was just a newline
        break            # Break out of the input loop
    user_input += line   # Keep adding input to variable

The other option I've been mentioning, even though I do not really like the assumptions underpinning this approach. 我提到的另一个选择,即使我不喜欢支撑这种方法的假设。 You can read your input and record time of each entered. 您可以读取每个输入的输入和记录时间。 You can define a time limit after which you are comfortable to assume it was not part of copy paste as a single block. 您可以定义一个时间限制,之后您可以认为它不是复制粘贴作为单个块的一部分。 And following newline on its own is then assumed to be end of user input. 然后假定自己跟随换行符结束用户输入。 For instance: 例如:

import sys
import time

COPY_PASTE_LIMIT = 0.5  # For instance 500ms
                        # Presuming platform time precision
                        # greater then whole seconds.

user_input = ''
last = 0  # We'll also later terminate when input was nothing
          # but an initial empty line.
for line in sys.stdin:
    now = time.time()
    if line == '\n' and (now - last > COPY_PASTE_LIMIT or last == 0):
        break
    last = now
    user_input += line

print(user_input)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM