Alex Rivera | Logout

Python - Read second column from file

Asked 2012-06-20T01:19:15.413
8

My input file has two columns. I am trying to print the second column of inputdata1.txt within a second for-loop. But my code is not working. Can someone tell me what should I do?

Edit
Report

1 Answer

8

You could do something like this. Separator is the character your file uses to separate colums, e.g. tabs or commas.

for line in open("inputfile.txt"):
    columns = line.split(separator)
    if len(columns) >= 2:
        print columns[1]
answered 2012-06-20T01:22:36.920

Your Answer