Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

python - 您如何从stdin阅读?(How do you read from stdin?)

I'm trying to do some of the code golf challenges, but they all require the input to be taken from stdin .

(我正在尝试进行一些代码挑战,但是它们都要求输入来自stdin 。)

How do I get that in Python?

(我如何在Python中得到它?)

  ask by tehryan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use the fileinput module:

(您可以使用fileinput模块:)

import fileinput

for line in fileinput.input():
    pass

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.

(fileinput将循环输入指定为命令行参数中给定的文件名的输入中的所有行,如果没有提供参数,则返回标准输入。)

Note: line will contain a trailing newline;

(注意: line包含尾随换行符;)

to remove it use line.rstrip()

(删除它使用line.rstrip())


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...