sys.stdin 中 readlines() 之后的 input()? [英] input() after readlines() from sys.stdin?
问题描述
我有一个案例,我的脚本在 sys.stdin
上调用 readlines()
,然后调用 input()
,但是最后一次通话无效.
这是我的脚本:
导入点击@click.command()@click.argument('data', type=click.File())定义输入(数据):行 = data.readlines()打印('读取 {} 行.继续?'.格式(len(行)))选择 = 输入().下()print("你说的是'{}'.".format(choice))如果 __name__ == '__main__':输入()
如果用户在命令行中指定了实际的文件参数,则脚本可以正常工作,但如果他们通过管道输入数据并使用 -
:
head -n10 data.txt |python3 script.py -阅读 10 行.继续?中止!
我需要支持 -
阅读,然后是 input()
-- 我怎样才能让它工作?
为了节省我们的时间,你可以自由跳到底部查看结论>
开始
我和你一样在寻找问题的答案.
首先,我认为以下可以解决问题...
<块引用>没有EOF的多个管道输入
但是,它在我的电脑上没有成功,因此,我试图弄清楚如何准确清除 EOF 标记...
<块引用>Terminate/Break/Abort Python Console/sys.stdin readline()
你如何终止/中断/中止 Python 控制台/sys.stdin readline()?
<块引用>冲洗
在 C 语言中,我们使用 fflush(stdin), ..etc 来清除缓冲区
在 C++ 语言中,我们使用 std::cin.get(), ..etc 来清除缓冲区
但是 Python 语言呢??
搜索了2个小时,发现Python目前没有提供这个功能,清除stdin中的buffer/EOF
以及像这样成功的在线可用答案如何使用`input()` 从`stdin` 读取文件后? 只适用于Linux 系统
<块引用>阅读后
https://www.twblogs.net/a/5c0ac824bd9eee6fb21399d4
*我非常确定(我认为)在 stdin 被文件采用后,Windows 中的 Python 不能使用 input
但是,这是否意味着我们应该打击 sys.stdin 方法?
不,显然,否则我不会花时间在这里打字...
这是我解决问题的最终方法
<块引用>重新创建您的自己的输入如下
def new_input(): ## 版本 1导入msvcrtstr_=''c=msvcrt.getche()而 ord(c)!=3 和 ord(c)!=4 和 ord(c)!=26 和 ord(c)!=13:# 打印(ord(c))str_ = str_+str(c)[2:-1]c=msvcrt.getche()返回 str_def new_input(interact_string_): ## 版本 2导入msvcrt打印(interact_string_,结束=")str_=''c=msvcrt.getche()而 ord(c)!=3 和 ord(c)!=4 和 ord(c)!=26 和 ord(c)!=13:# 打印(ord(c))str_ = str_+str(c)[2:-1]c=msvcrt.getche()返回 str_def new_input(interact_string_): ## 版本 3导入msvcrt导入操作系统打印(interact_string_,结束=")str_=''打印('')c=msvcrt.getche()而 ord(c)!=3 和 ord(c)!=4 和 ord(c)!=26 和 ord(c)!=13:os.system('cls')打印(interact_string_,结束=")# 打印(ord(c))如果 ord(c)==8:str_ = str_[0:-1]打印(str_)别的:str_ = str_+str(c)[2:-1]打印(str_)c=msvcrt.getche()# 打印(str_)返回 str_
这里是数字的含义
- ord(c)==3 将是 Ctrl+C 的键
- ord(c)==4 将是 Ctrl+D 的键
- ord(c)==26 将是 Ctrl+Z 的键
- ord(c)==13 将是 Enter 的键
- ord(c)==22 将是 Ctrl+C 的键
- ord(c)==8 将成为退格键
使用if-structure来反应还是很方便的,返回最后的字符串
结论:
在使用 stdin 到 readlines()/readline()/read() 读取文件后,您永远无法在 Windows 系统中使用 stdin 输入/输出>
虽然在同样的情况下,Linux 系统上的相同问题是可以解决的,答案就在那里......
在我看来,根据我的研究,Windows 的最佳答案将是我的 (我当然会这么说..) .++==>重新创建您的输入()
感谢您的阅读,欢迎建设性的建议和礼貌的评论.也欢迎任何有更好想法或解决方案的人.
喜欢这个旅程和我一起阅读寻找答案的冒险
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.
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 -
:
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?
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
How to use `input()` after reading a file from `stdin`?
however, it does not succeed in my computer, and thus, I try to figure out how to exactly clear the EOF mark...
Terminate/Break/Abort Python Console/sys.stdin readline()
How do you terminate/break/abort a Python console/sys.stdin readline()?
flush
How to flush the input stream in python?
In C Language, we use fflush(stdin), ..etc to clear the buffer
In C++ Language, we use std::cin.get(), ..etc to clear the buffer
But How About Python Language ??
After Searching For 2 hours, I realize Python so far does not provide this function, clear the buffer/EOF in the stdin
And the answers available online which succeed like this one How to use `input()` after reading a file from `stdin`? are only suitable for Linux System
After Reading This
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
However, does that mean, we should batter the sys.stdin method?
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)==4 will be the key of Ctrl+D
- ord(c)==26 will be the key of Ctrl+Z
- ord(c)==13 will be the key of Enter
- ord(c)==22 will be the key of Ctrl+C
- ord(c)==8 will be the key of Backspace
And it will still be convenient to using if-structure to react with, and returns the final string
Conclusion:
after using stdin to readlines()/readline()/read() to read a file you cannot ever using stdin to input/output in Windows System
while, in the same case, the Same Problem on Linux System is solvable, and the answer will be up there...
In my opinion and under my research, The Best Answer to Windows will be mine (of course will I say that..) . ++==> 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
这篇关于sys.stdin 中 readlines() 之后的 input()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!