简体   繁体   English

如何完成sys.stdin.readlines()输入?

[英]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 + ZCTRL + D **都不适合您 ,并且您正在漫游正在发生的事情,请执行以下操作:

  • check if you are using default english keyboard layout 检查您是否使用默认的英文键盘布局
  • if you do have different, non-default keyboard layout try switching keyboard setting to English in language bar, then try pressing ctrl + z after changes 如果您有其他非默认的键盘布局,请尝试在语言栏中将键盘设置切换为英语,然后在更改后尝试按ctrl + z
  • if you're still confused look at the screen, what appears in command line when you press ctrl + z. 如果仍然感到困惑,请查看屏幕,当您按ctrl + z时,命令行中会显示什么。 What symbol do you see? 您看到什么符号? When I was pressing ctrl + z I was seeing this: ^Y, and when by mistake I pressed ctrl + y I've seen this ^Z, i pressed enter and the input was taken, EOF sent. 当我按ctrl + z时,我看到的是:^ Y,而当我误按ctrl + y时,我看到了这个^ Z,我按enter并输入了内容,EOF已发送。

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.

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