Sunday 15 August 2010

python - How to read through a text file in row and column wise manner -



python - How to read through a text file in row and column wise manner -

i have text file info in 3 columns, such

1 1 -1 1 -1 -1 -1 1 1 1 1 1 -1 1 1

now, want check if row contains @ to the lowest degree 1 "1", if print "1" in output file corresponding row. if none of columns in row contain "1", print "-1" in output file corresponding row.

i not know how process text file using row*column wise manner. can u help me process it.

this programme iterates on rows of input file, emitting either "1" or "-1" each row.

for row in input_file loops on of rows, assigning each of them variable row in turn.

row.split() creates array of strings, each string represents 1 column in row.

if '1' in row.split() true @ to the lowest degree 1 of items returned row.split() '1'.

with open('input.txt') input_file: row in input_file: if '1' in row.split(): print 1 else: print -1

python python-2.7

No comments:

Post a Comment