python输入eof结束_如何在python中的EOF之前读取用户输入?

I came across this problem in UVa OJ. 272-Text Quotes

Well, the problem is quite trivial. But the thing is I am not able to read the input. The input is provided in the form of text lines and end of input is indicated by EOF.

In C/C++ this can be done by running a while loop:

while( scanf("%s",&s)!=EOF ) { //do something }

How can this be done in python .?

I have searched the web but I did not find any satisfactory answer.

Note that the input must be read from the console and not from a file.

解决方案

You can use sys module:

import sys

complete_input = sys.stdin.read()

sys.stdin is a file like object that you can treat like a Python File object.

From the documentation:

Help on built-in function read:

read(size=-1, /) method of _io.TextIOWrapper instance

Read at most n characters from stream.

Read from underlying buffer until we have n characters or we hit EOF.

If n is negative or omitted, read until EOF.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值