如何完成sys.stdin.readlines()输入? [英] How to finish sys.stdin.readlines() input?

查看:669
本文介绍了如何完成sys.stdin.readlines()输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但是由于找不到答案,我不得不提出这个问题.

This might be a silly question, but as I can't find an answer, I have to ask it.

在交互式python中,我想处理一条消息:

In interactive python I want to process a message which i get with:

>>> message = sys.stdin.readlines()

一切正常,但是...如何阻止它获取输入并将其保存到message变量中?使用ctrl + c停止将停止整个过程,因此没有输入要保存在任何地方.我想我找不到一个简单的答案...

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...

推荐答案

对于基于Unix的系统:

您好,您可以使用磁带: Ctrl d

Hello, you can tape : Ctrld

Ctrl d 通过发送示例:

>>> 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']

对于Windows:

要在Windows上发送EOF,您可以将 Ctrl d 替换为 Ctrl z

To send EOF on Windows, you can replace Ctrld by Ctrlz

这篇关于如何完成sys.stdin.readlines()输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆