简体   繁体   English

来自sys.stdin的readlines()之后的input()?

[英]input() after readlines() from sys.stdin?

I have a case where my script calls readlines() on sys.stdin followed by a call to input() , but that last call won't work.我有一个案例,我的脚本在sys.stdin上调用readlines() ,然后调用input() ,但最后一次调用不起作用。

Here's my script:这是我的脚本:

import click

@click.command()
@click.argument('data', type=click.File())
def inp(data):
    lines = data.readlines()
    print('Read {} lines. Continue?'.format(len(lines)))
    choice = input().lower()
    print("You said '{}'.".format(choice))

if __name__ == '__main__':
    inp()

The script works fine if the user specified an actual file argument on the command line, but not if they pipe input data and use the - :如果用户在命令行上指定了实际的文件参数,则脚本可以正常工作,但如果他们 pipe 输入数据并使用-则不会:

head -n10 data.txt | python3 script.py -  
Read 10 lines. Continue?
Aborted!

I need to support the - reading followed by input() -- how can I make it work?我需要支持-阅读后跟input() - 我怎样才能让它工作?

to save our time, u are free to jump to the bottom to see the conclusion为了节省我们的时间,你可以自由跳到底部看结论

Start开始

I am searching for the answer to the question as u did.我正在像你一样寻找问题的答案。

At First, I think the following will solve the problem...首先,我认为以下将解决问题......

multiple piped inputs without EOF没有 EOF 的多个管道输入

How to use `input()` after reading a file from `stdin`? 从`stdin`读取文件后如何使用`input()`?

however, it does not succeed in my computer, and thus, I try to figure out how to exactly clear the EOF mark...但是,它在我的计算机上没有成功,因此,我试图弄清楚如何准确清除 EOF 标记......

Terminate/Break/Abort Python Console/sys.stdin readline()终止/中断/中止 Python 控制台/sys.stdin readline()

How do you terminate/break/abort a Python console/sys.stdin readline()? 如何终止/中断/中止 Python 控制台/sys.stdin readline()?

flush冲洗

How to flush the input stream in python? 如何刷新 python 中的输入 stream?

In C Language, we use fflush(stdin), ..etc to clear the buffer在 C 语言中,我们使用 fflush(stdin), ..etc 来清除缓冲区

In C++ Language, we use std::cin.get(), ..etc to clear the buffer在 C++ 语言中,我们使用 std::cin.get(), ..etc 来清除缓冲区

But How About Python Language??但是 Python 语言怎么样?

After Searching For 2 hours, I realize Python so far does not provide this function, clear the buffer/EOF in the stdin搜索2小时后,我意识到Python到目前为止不提供这个function,清除标准输入中的缓冲区/EOF

And the answers available online which succeed like this one How to use `input()` after reading a file from `stdin`?并且在线提供的答案像这样成功如何从`stdin`读取文件后使用`input()`? are only suitable for Linux System仅适用于 Linux 系统

After Reading This看完这篇

https://www.twblogs.net/a/5c0ac824bd9eee6fb21399d4 https://www.twblogs.net/a/5c0ac824bd9eee6fb21399d4

*I am so sure(i think) that Python in Windows cannot use input after stdin is taken by file *我很确定(我认为)Windows 中的 Python 在文件采用标准输入后无法使用输入

However, does that mean, we should batter the sys.stdin method?但是,这是否意味着我们应该使用 sys.stdin 方法?

No, Obviously, or I won't spend my time here typing...不,显然,否则我不会花时间在这里打字......

And Here Is My Final Method To Solve The Problem这是我解决问题的最终方法

re-Create Your Own Input as follow重新创建您自己的输入如下

def new_input(): ## version 1
import msvcrt
str_=''
c=msvcrt.getche()
while ord(c)!=3 and ord(c)!=4 and ord(c)!=26 and ord(c)!=13:
    # print(ord(c))
    str_ = str_+str(c)[2:-1]
    c=msvcrt.getche()
return str_

def new_input(interact_string_): ## version 2
import msvcrt
print(interact_string_, end ="")
str_=''
c=msvcrt.getche()
while ord(c)!=3 and ord(c)!=4 and ord(c)!=26 and ord(c)!=13:
    # print(ord(c))
    str_ = str_+str(c)[2:-1]
    c=msvcrt.getche()
return str_

def new_input(interact_string_): ## version 3
import msvcrt
import os
print(interact_string_, end ="")
str_=''
print('')
c=msvcrt.getche()
while ord(c)!=3 and ord(c)!=4 and ord(c)!=26 and ord(c)!=13:
    os.system('cls')
    print(interact_string_, end ="")
    # print(ord(c))
    if ord(c)==8:
        str_ = str_[0:-1]
        print(str_)
    else:
        str_ = str_+str(c)[2:-1]
        print(str_)
    c=msvcrt.getche()
# print(str_)
return str_

Here, are the meanings of numbers这里是数字的含义

  • ord(c)==3 will be the key of Ctrl+C ord(c)==3 将是Ctrl+C的键
  • ord(c)==4 will be the key of Ctrl+D ord(c)==4 将是Ctrl+D的键
  • ord(c)==26 will be the key of Ctrl+Z ord(c)==26 将是Ctrl+Z的键
  • ord(c)==13 will be the key of Enter ord(c)==13 将是Enter的键
  • ord(c)==22 will be the key of Ctrl+C ord(c)==22 将是Ctrl+C的键
  • ord(c)==8 will be the key of Backspace ord(c)==8 将是Backspace的键

And it will still be convenient to using if-structure to react with, and returns the final string并且使用if-structure进行反应仍然很方便,并返回最终字符串


Conclusion:结论:

  1. after using stdin to readlines()/readline()/read() to read a file you cannot ever using stdin to input/output in Windows System在使用 stdin 到 readlines()/readline()/read() 读取文件后,您永远无法Windows 系统中使用 stdin 输入/输出

  2. while, in the same case, the Same Problem on Linux System is solvable, and the answer will be up there...同时,在同样的情况下, Linux 系统上的相同问题是可以解决的,答案将在那里......

  3. In my opinion and under my research, The Best Answer to Windows will be mine (of course will I say that..) .在我看来并根据我的研究, Windows的最佳答案将是的(当然我会这么说..) ++==> re-Create your input() ++==>重新创建您的输入()

Thanks For Your Reading, constructive advice and polite comment is welcomed.感谢您的阅读,欢迎有建设性的建议和礼貌的评论。 Anyone With a Better Idea or Solve is welcome too.也欢迎任何有更好想法或解决方案的人。

like it if u enjoy the journey with me while reading my adventure to finding answer如果你喜欢和我一起享受旅程,同时阅读我的冒险寻找答案

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

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