从 sys.stdin 读取,以 RETURN 结束用户输入 [英] Reading from sys.stdin, ending user input with RETURN

查看:72
本文介绍了从 sys.stdin 读取,以 RETURN 结束用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Py 3.6 中的用户输入编写脚本.

I am working on a script with user input in Py 3.6.

在脚本中,用户被要求在 shell 中输入一个文本部分 - 可能包含新行.然后输入的文本将保存到 Python 变量中以供进一步处理.

In the script, the user is asked to enter a text section - potentially containing new lines - into the shell. The entered text will then be saved to a Python variable for further processing.

由于用户输入可能包含换行符,我想我不能使用 input() 但我正在使用 sys.stdin.read()(如建议的 此处).

Since the user input may contain newlines, I think I cannot use input() but am using sys.stdin.read() (as suggested here).

读入输入工作正常,但要结束用户输入,用户必须按回车键,然后使用组合键CTRL + d(见此处).(请参阅下面的当前程序)

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). (See Current Procedure below)

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

也欢迎使用 CTRL + d 对当前流程进行任何其他简化.

Any other simplification to the current process with CTRL + d is appreciated as well.

  • 这可行吗?

  • Is this doable?

这里有一些技巧,但我想也许有是更好的方法

There are some hacks here but I thought maybe there is a better way

    # 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) 粘贴文本2) 点击 RETURN 结束输入3) 按 Ctrl+d 移动到下一个文件

1) paste the text 2) hit RETURN to end input 3) hit Ctrl+d to move to next file

我想将其简化为:

1) 粘贴文本2) 点击RETURN 结束输入并移动到下一个文件

1) paste the text 2) hit RETURN to end input and move to next file

在 MacOSX 上运行 Python 3.5.6,使用终端进行文本输入.非常感谢任何帮助!

Running Python 3.5.6 on MacOSX, using Terminal for text input. Any help is much appreciated!

推荐答案

根据你在评论中的回复,如果以空行终止是可以接受的(即你的输入文本不能单独包含换行符,除了终止输入),那引用微不足道:

Based on your reply in the comment, if terminating by empty line is acceptable (i.e. 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)

这篇关于从 sys.stdin 读取,以 RETURN 结束用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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