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?
555
145
| |
454
|
This is something I learned from Stack Overflow:
| ||||||||||||||||||||
|
302
|
There's a few ways to do it.
You will probably find this Wikibook article on I/O in Python to be a useful reference as well. | ||||||||||||
|
135
|
| ||||||||||||||||
|
88
|
Here's from Learning Python:
On Unix, you could test it by doing something like:
On Windows or DOS, you'd do:
| ||||||||
|
74
|
Python also has built-in functions, For example,
or
| |||
53
|
The answer proposed by others:
is very simple and pythonic, but it must be noted that the script will wait until EOF before starting to iterate on the lines of input. This means that The correct script for such a use case would be:
UPDATE | ||||||||||||||||||||
|
20
|
This will echo standard input to standard output:
| |||
7
|
Building on all the anwers using
and use it as either
or
or even
That would make your Python script behave like many GNU/Unix programs such as | |||
3
|
You can read from stdin and then store inputs into "data" as follows:
| |||
2
|
Try this:
|
raw_input()
orsys.stdin
(and the latter seems more appropriate) but there are subtle differences between the two that I thought the following post covered pretty well: albertech.blogspot.com/2015/02/… – jar Feb 27 at 3:46