[英]How to finish sys.stdin.readlines() input?
This might be a silly question, but as I can't find an answer, I have to ask it. 这可能是一个愚蠢的问题,但是由于找不到答案,我不得不提出这个问题。
In interactive python I want to process a message which i get with: 在交互式python中,我想处理一条消息:
>>> message = sys.stdin.readlines()
Everything works fine, but... how to stop it from getting an input and make it save into message variable? 一切正常,但是...如何阻止它获取输入并将其保存到message变量中? Stopping with ctrl+c stops whole process so there is no input to be saved anywhere.
使用ctrl + c停止将停止整个过程,因此没有输入要保存在任何地方。 I guess there's an easy answer I just can't find...
我想我找不到一个简单的答案...
For unix based system : 对于基于Unix的系统:
Hello, you can tape : Ctrl d 您好,您可以录音: Ctrl d
Ctrl d closes the standard input (stdin) by sending EOF . Ctrl d通过发送EOF关闭标准输入(stdin)。
Example : 范例:
>>> import sys
>>> message = sys.stdin.readlines()
Hello
World
My
Name
Is
James
Bond
# <ctrl-d> EOF sent
>>> print message
['Hello\n', 'World\n', 'My\n', 'Name\n', 'Is\n', 'James\n', 'Bond\n']
For Windows : 对于Windows:
To send EOF on Windows, you can replace Ctrl d by Ctrl z 要在Windows上发送EOF,您可以将Ctrl d替换为Ctrl z
This is an old question but it needs an update about Windows and different keyboard layouts. 这是一个老问题,但是需要有关Windows和不同键盘布局的更新。
If neither CTRL + Z nor CTRL + D ** work for you on Windows and and you're wandering what is going on do this: 如果在Windows上 CTRL + Z或CTRL + D **都不适合您 ,并且您正在漫游正在发生的事情,请执行以下操作:
This is somewhat strange and counterintuitive. 这有点奇怪并且违反直觉。 I changed keys layout some time ago to include polish characters, but all the common keys are left unchanged, z still maps to z when I use the keyboard normally, normally ctrl + z does nothing in my keyboard, so I shouldn't be changed.
不久前,我更改了键的布局以包含波兰语字符,但是所有常用键均保持不变,当我正常使用键盘时,z仍映射到z,通常ctrl + z在键盘上什么也不做,所以不应该更改。 But apparently in cmd it works differently, in order to have default link between ctrl and z I have to switch to default layout, or use control y to sent EOF.
但是显然在cmd中它的工作方式不同,为了在ctrl和z之间具有默认链接,我必须切换到默认布局,或使用控件y发送EOF。
Use CTRL-D
. 使用
CTRL-D
。
message = sys.stdin.readlines()
abc
def
<CTRL-D>
# message == ['abc\n', 'def\n']
在Windows上,只需执行CTRL + Z,然后按Enter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.