[Tutor] Read a matrix with lines in different behavior
Felipe Melo
felipe146 at hotmail.com
Fri May 23 14:23:41 CEST 2014
Hello,
I want to read the below matrix, identify when the characters in front of "want = " are equal to "1" and then save in an array and in an output file the characters above. But I don't know how to identify the second line and store in a variable:
alpha=0 beta=2 gamma=50
want = 0
alpha=0 beta=2 gamma=50
want = 1
alpha=0 beta=2 gamma=50
want = 0
alpha=0 beta=2 gamma=50
want = 1
alpha=0 beta=2 gamma=50
want = 0
This is part of the code:
try:
datadir = '/home/me/Test_python/'
fileHandle = open( "%s/teste.txt"%datadir, 'r'
)
vector = [ ]
for line in fileHandle.readlines():
line=line.strip()
a = line.split(" ")[0]
print a
b = line.split(" ")[1]
print b
c = line.split(" ")[2]
print c
d = line.split(" ")[3]
print d
if d == "1":
vector.append([a,b,c])
n = n + 1
fileHandle.close()
file = open("saida.txt","w")
for cont in range(n):
file.write = vector
except:
print "Exception"
sys.exit( 1 )
When I execute the code there is an error when the loop finds the second line
[felipe at grumari Test_python]$ python verificar.py
alpha=0
beta=2
gamma=50
Exception
Thanks,
Felipe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140523/cc3a695a/attachment.html>
More information about the Tutor
mailing list