如何读取用户输入直到EOF? [英] How to read user input until EOF?

查看:139
本文介绍了如何读取用户输入直到EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的代码读取用户输入,直到换行。
但是我正在尝试将其更改为一种格式,在该格式下,用户可以写入输入,直到strg + d结束输入。



我目前这样做

  input = raw_input( Input:)

但是如何将其更改为支持EOF的版本?

解决方案

使用 file.read

  input_str = sys.stdin.read()

根据文档:


file.read([大小])



最多从文件读取大小字节(如果读取在获得大小字节之前达到EOF
,则更少)。如果size参数为负或省略
,则读取所有数据,直到达到EOF。




 >>> import sys 
>> isinstance(sys.stdin,file)

BTW,不要使用 input 作为变量名。它隐藏了内置函数 input 。 / p>

My current code reads user input until line-break. But I am trying to change that to a format, where the user can write input until strg+d to end his input.

I currently do it like this:

input = raw_input ("Input: ")

But how can I change that to an EOF-Ready version?

解决方案

Use file.read:

input_str = sys.stdin.read()

According to the documentation:

file.read([size])

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached.

>>> import sys
>>> isinstance(sys.stdin, file)
True

BTW, dont' use input as a variable name. It shadows builtin function input.

这篇关于如何读取用户输入直到EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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