如何在EOF之前读取用户输入? [英] How to read user input until EOF?

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

问题描述

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

我目前是这样做的:

input = raw_input("输入:")

但是如何将其更改为 EOF-Ready 版本?

解决方案

使用 file.read:

input_str = sys.stdin.read()

根据文档:

<块引用>

file.read([size])

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

<预><代码>>>>导入系统>>>isinstance(sys.stdin,文件)真的

顺便说一句,不要使用 input 作为变量名.它隐藏了内置函数 input.

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天全站免登陆