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?
转载于:https://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-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?
转载于:https://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python
There's a few ways to do it.
sys.stdin
is a file-like object on which you can call functions read
or readlines
if you want to read everything or you want to read everything and split it by newline automatically. (You need to import sys
for this to work.)
If you want to prompt the user for input, you can use raw_input
in Python 2.X, and just input
in Python 3.
If you actually just want to read command-line options, you can access them via the sys.argv list.
You will probably find this Wikibook article on I/O in Python to be a useful reference as well.