I am reading input for my python program from stdin (I assigned a stdin file object). The number of input lines is not known in advance. Sometimes a program can get 1 line, 100 lines, or even no lines.
import sys sys.stdin = open ("Input.txt") sys.stdout = open ("Output.txt", "w") def main(): for line in sys.stdin: print line main()
This is the closest to my requirement. But this is a problem. If the input signal
3 7 4 2 4 6 8 5 9 3
he prints
3 7 4 2 4 6 8 5 9 3
It prints an extra line of new line after each line. How to fix this program or how to best solve this problem?
EDIT: Here is an example launch http://ideone.com/8GD0W7
EDIT2: Thanks for the answer. I found out about the error.
import sys sys.stdin = open ("Input.txt") sys.stdout = open ("Output.txt", "w") def main(): for line in sys.stdin: for data in line.split(): print data, print "" main()
Such a program has been changed, and it works as expected. :)
source share