你如何从标准输入读取? [英] How do you read from stdin?

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

问题描述

我正在尝试进行一些代码高尔夫挑战,但它们都需要从 stdin 获取的输入.我如何在 Python 中获得它?

I'm trying to do some of the code golf challenges, but they all require the input to be taken from stdin. How do I get that in Python?

推荐答案

你可以使用 fileinput 模块:

You could use the fileinput module:

import fileinput

for line in fileinput.input():
    pass

fileinput 将遍历输入指定为命令行参数中给出的文件名,如果没有提供参数,则为标准输入.

fileinput will loop through all the lines in the input specified as file names given in command-line arguments, or the standard input if no arguments are provided.

注意:line 将包含一个尾随换行符;要删除它,请使用 line.rstrip()

Note: line will contain a trailing newline; to remove it use line.rstrip()

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

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