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

时间:2021-08-02 07:11:05

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? Stopping with ctrl+c stops whole process so there is no input to be saved anywhere. I guess there's an easy answer I just can't find...

一切都很好,但是……如何阻止它获取输入并使其保存到消息变量中?停止使用ctrl+c停止整个进程,因此没有输入可以保存到任何地方。我想有一个简单的答案我就是找不到……

4 个解决方案

#1


38  

For unix based system :

基于unix的系统:

Hello, you can tape : Ctrld

你好,你可以用带子。

Ctrld closes the standard input (stdin) by sending EOF.

通过发送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 Ctrld by Ctrlz

要在Windows上发送EOF,您可以用ctrl - z替换ctrl ld。

#2


4  

Use CTRL-D.

使用ctrl - d。

message = sys.stdin.readlines()
abc
def
<CTRL-D>

# message == ['abc\n', 'def\n']

#3


4  

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:

如果没有CTRL + Z或CTRL + D **在Windows上为你工作,你就会徘徊在做什么事情:

  • 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. 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时,命令行中出现了什么?你看到了什么符号?我按ctrl + z的时候看到这个:^ Y,误当我按下ctrl + Y我见过这个^ z,我按输入和输入拍摄,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. 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.

这有点奇怪和违反直觉。我在前一段时间修改了键盘布局,包括了波兰字符,但是所有的通用键都没有变化,z仍然映射到z,当我正常使用键盘时,通常ctrl + z在键盘上什么都不做,所以我不应该改变。但显然在cmd中它的工作方式不同,为了在ctrl和z之间有默认的链接,我必须切换到默认的布局,或者使用control y发送EOF。

#4


1  

On windows simply do CTRL+Z and press enter

在windows上,只需按CTRL+Z并按enter键。

#1


38  

For unix based system :

基于unix的系统:

Hello, you can tape : Ctrld

你好,你可以用带子。

Ctrld closes the standard input (stdin) by sending EOF.

通过发送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 Ctrld by Ctrlz

要在Windows上发送EOF,您可以用ctrl - z替换ctrl ld。

#2


4  

Use CTRL-D.

使用ctrl - d。

message = sys.stdin.readlines()
abc
def
<CTRL-D>

# message == ['abc\n', 'def\n']

#3


4  

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:

如果没有CTRL + Z或CTRL + D **在Windows上为你工作,你就会徘徊在做什么事情:

  • 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. 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时,命令行中出现了什么?你看到了什么符号?我按ctrl + z的时候看到这个:^ Y,误当我按下ctrl + Y我见过这个^ z,我按输入和输入拍摄,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. 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.

这有点奇怪和违反直觉。我在前一段时间修改了键盘布局,包括了波兰字符,但是所有的通用键都没有变化,z仍然映射到z,当我正常使用键盘时,通常ctrl + z在键盘上什么都不做,所以我不应该改变。但显然在cmd中它的工作方式不同,为了在ctrl和z之间有默认的链接,我必须切换到默认的布局,或者使用control y发送EOF。

#4


1  

On windows simply do CTRL+Z and press enter

在windows上,只需按CTRL+Z并按enter键。