Monday 4 February 2013

Multiple inputs in one line in Python

import re
array=[]
string = raw_input()
temp = re.sub("[^\w]", " ", string).split()
for element in temp:
    array.append(int(element))
    print element               

How ot read a file till EOF in Python

You might be wondering of how to take read a file till EOF in Python. Actually, its quite easy, but most of the Python users faces this problem initially.

Solution:
Use the following piece of code in your program. It will read the input_file till EOF and stores the data of the input file as a string.


while True:
    try:
        a= raw_input()
        #do something...      
    except EOFError:
        break


You can input your input_file from the terminal using following command:
            python your_code.py < input_filename

To get your output in another file:
          python your_code.py < input_filename > output_filename