On 23/05/2014 13:23, Felipe Melo wrote:
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@grumari Test_python]$ python verificar.py
alpha=0
beta=2
gamma=50
Exception


You've made a classic newbie mistake by catching all exceptions. Take the try/except out of your code and Python will tell you precisely where your problem actually is. Try this and if you can't make any progress please get back to us. You also only need one call to line.split, not four, and I'm unsure whather or not the call to vector.append is what you actually want.

--
My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to