[Tutor] Read a matrix with lines in different behavior
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 Thanks, Felipe ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Doubts about installing python3.1 in squeeze
On 22-05-2014 13:22, Alex Kleider wrote: On 2014-05-22 06:17, Markos wrote: Hi, I'm learning Python and I'm using Debian 6.0 (squeeze) The installed version is 2.6.6. (python -V) I have seen some recommendations for beginners to invest in version 3. I found package of Python 3.1 in repository for squeeze. I am considering installing Python 3.1 with apt-get install python3.1 But I found the site http://www.circuidipity.com/python2-and-python3.html information on how to keep the two versions using virtualenv. Also I found in the /usr/bin python2.5 and python2.6 And in /usr/lib python2.4, python2.5 and python2.6 Can I just run apt-get install python3.1 or should I do any other configuration? I'm confused. Are there any risk to install python3.1 and some programs stop working on my debian squeeze? Thanks for any tips? Markos On Ubuntu both v2 and v3 are installed by default. Have you tried typing python3 on the command line? If you get the interpreter, it's installed. Then you just have to use a different shebang line in your code files: #!/usr/bin/env python3 I don't have a debian system close at hand to test this myself. alex ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Dear, Thanks everyone for the tips. I tried to install python3.1 with the command apt-get install python3.1 and use the shebang #!/usr/bin/env python3 in the source code header. It works. But to use Tkinter I also had to install the package python3-tk apt-get intall python3-tk Now I have in /usr/bin python python3 python2.5 python2.6 python3.1 python is a link to python2.6 and python3 is a link to python3.1 Until now everything is working. Markos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock
Wondering if anyone came across this error in using form = cgi.FieldStorage() import tempfile File /tempfile.py", line 83, in _once_lock = _allocate_lock() thread.error: can't allocate lock puthon version 2.7, on HP-UX 11.11 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read a matrix with lines in different behavior
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
Re: [Tutor] cgi.FieldStorage() causing thread.error: can't allocate lock
On 23/05/14 12:57, SABARWAL, SHAL wrote: Wondering if anyone came across this error in using form = cgi.FieldStorage() import tempfile File /tempfile.py", line 83, in _once_lock = _allocate_lock() thread.error: can't allocate lock puthon version 2.7, on HP-UX 11.11 Since we can't see the code in tempfile.py we could ony guess Also please include the full error traceback, that looks like you have summarised it. As it stands it is impossible to tell how tempfile.py relates to cgi.FieldStorage() or even if the error is related to that at all. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Read a matrix with lines in different behavior
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 Felipe you're shooting yourself in the foot when you put the try: ... # error occurs here except: # catch everything ... # show a generic message around the part of the code that does the actual work. Remove the try...except, and see if you can make sense of the traceback and the error message you get. The message will say what exactly went wrong, and the traceback will show where it went wrong. If with this valuable information you still cannot fix the code yourself come back here, but this time post the traceback (use copy and paste, don't rephrase) to make it easier for us to help you fix the bug. Random hints: - your code seems to expect > alpha=0 beta=2 gamma=50 > want = 0 on a single line. Either adjust the data or the code. - writing to a file is spelt file.write(some_string) - you can iterate over lists, no need for range(): for item in items: ... # do something with item - you can iterate over files, no need for readlines(): for line in file: ... # do something with line ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor