Re: [Tutor] best way to get external data
PyYaml is an option too. [1] [1] - http://pyyaml.org/ It´s very readable and is converted to a Python native structure/object. On Fri, May 2, 2008 at 12:12 PM, Bryan Fodness <[EMAIL PROTECTED]> wrote: > I am trying to figure out the best way to get external data. > > Using the following data in a file > > 1 > 2 > 3 > > I have used, > > fi = open(infile, 'r') > s = fi.readlines() > fi.close() > a = s[0] > b = s[1] > c = s[2] > > but, if I have, > > x = 1 > y = 2 > z = 3 > > I have used, > > for line in open(infile): > i_line = line.split() > if i_line[0] == 'x': >a = i_line[2] >elif i_line[0] == 'y': >b = i_line[2] >elif i_line[0] == 'z': >c = i_line[2] > > I do have control over the creation of the data file. The second way is > better in my mind, because it is independent of the ordering. > > If I have a long list of 'variable' = 'value', could this be better > managed in a dictionary? Or, is there a better way? > > Thanks > > > -- > "The game of science can accurately be described as a never-ending insult > to human intelligence." - João Magueijo > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- Meu nome é Valdo, Ruivaldo Neto. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Starting a .py file from Idle
Mihai Iacob wrote: Hello, Can anyone tell me how to start a program directly from the interpreter (i'm using IDLE). Usually i open a new window , write the lines of code and press F5 to run the program in the interpreter. The problem is that i need to to that directly from the interpreter. Pray tell, why? Try >>> import program -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python -v command
"Stephanie" <[EMAIL PROTECTED]> wrote programs via the Macintosh Unix Terminal. I was trying to quickly see which version of Python I am running and typed in the command "python -v". uppercase: python -V Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Starting a .py file from Idle
"Mihai Iacob" <[EMAIL PROTECTED]> wrote Can anyone tell me how to start a program directly from the interpreter (i'm using IDLE). Usually i open a new window , write the lines of code and press F5 to run the program in the interpreter. You mean the results show up in the interactive shell window of IDLE? The problem is that i need to to that directly from the interpreter. (I'm running windows). Do you mean you want to run a program you have previously written inside IDLE? Or that you want to run a program outside of IDLE? Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Starting a .py file from Idle
> On Fri, May 2, 2008 at 7:11 PM, Mihai Iacob <[EMAIL PROTECTED]> wrote: > Hello, > > Can anyone tell me how to start a program directly > from the interpreter (i'm using IDLE). Usually i open > a new window , write the lines of code and press F5 to > run the program in the interpreter. > The problem is that i need to to that directly from > the interpreter. (I'm running windows). FILE > OPEN would open any text file & F5 would run any code in there. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Starting a .py file from Idle
i guess : START > RUN then type : python your_script.py arg_1 arg_2 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Starting a .py file from Idle
Hello, Can anyone tell me how to start a program directly from the interpreter (i'm using IDLE). Usually i open a new window , write the lines of code and press F5 to run the program in the interpreter. The problem is that i need to to that directly from the interpreter. (I'm running windows). Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] put? and files
On Fri, May 2, 2008 at 3:08 PM, Ross Glover <[EMAIL PROTECTED]> wrote: > I'm assuming this falls under the rubric of text parsing. Here's what I > want to make: > A way to create a set of user defined tags or markers that can be applied > to any section of a text document. Then I want a function that will write > to a file all of the tagged/marked text (that is text between tag_start and > tag_end), preferably with a reference to the file from which it came. You could do that with just string find and slicing but I think it will be simpler with regular expressions. You can make a regex to match everything from 'tag_start' to 'tag_end' and use a group to pull out the stuff in between. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] put? and files
If you give some examples of what you want to parse we can give more specific advice. Kent I'm assuming this falls under the rubric of text parsing. Here's what I want to make: A way to create a set of user defined tags or markers that can be applied to any section of a text document. Then I want a function that will write to a file all of the tagged/marked text (that is text between tag_start and tag_end), preferably with a reference to the file from which it came. Ultimately, I want this to reside in a gui which allows the user to read the text, tag/mark various sections of interest, and then write all of those sections to files corresponding to the specified tags. I expect the files to be between 50k and 150k words. thanks, ross -- If you know what you're doing, you must not be 'doing' a dissertation. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Method question?
I think I'm beginning to understand how classes/methods work now, I'm sure further understanding will come with practice. Thanks for the help and suggestions! -Wayne On Fri, May 2, 2008 at 1:42 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > On Fri, May 2, 2008 at 2:20 PM, W W <[EMAIL PROTECTED]> wrote: > > > I knew/guessed the alias bit from my experience with C++, but I > > couldn't figure out exactly what I needed. I've seen the "self" > > reference before, but I never really understood it. > > 'self' is roughly like 'this' in C++. Unlike C++, self must be > explicit - it is listed as a method parameter and it must be used for > attribute access. > > > > Let me see if I > > understand correctly: > > > > class myClass(): > > or, more idiomatic (class names start with upper case letters) and > modern (inherit from object to create a new-style class): > class MyClass(object): > > > > > creates a new "data type"(?), called myClass, and > > > > x = myClass() > > > > creates a variable with the type of "myClass", similar to foo = dict() > > > > and the method inside myClass defined as myMethod(self), can be called > > on the data type "myClass" like this: > > > > x.myMethod() > > > > is that fairly accurate? > > Yes. > > Kent > -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
bob gailer wrote: Or even simplre f = open('file.txt',r).readlines() print [f[x+1] for x, line in enumerate(f) if line.rstrip() == "3"][0] -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Method question?
On Fri, May 2, 2008 at 2:20 PM, W W <[EMAIL PROTECTED]> wrote: > I knew/guessed the alias bit from my experience with C++, but I > couldn't figure out exactly what I needed. I've seen the "self" > reference before, but I never really understood it. 'self' is roughly like 'this' in C++. Unlike C++, self must be explicit - it is listed as a method parameter and it must be used for attribute access. > Let me see if I > understand correctly: > > class myClass(): or, more idiomatic (class names start with upper case letters) and modern (inherit from object to create a new-style class): class MyClass(object): > > creates a new "data type"(?), called myClass, and > > x = myClass() > > creates a variable with the type of "myClass", similar to foo = dict() > > and the method inside myClass defined as myMethod(self), can be called > on the data type "myClass" like this: > > x.myMethod() > > is that fairly accurate? Yes. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Method question?
i had tough time understanding classes ... hope this helps : http://www2.lib.uchicago.edu/keith/courses/python/class/5/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Method question?
On Fri, May 2, 2008 at 12:17 PM, Kent Johnson <[EMAIL PROTECTED]> wrote: > This just makes x an alias for the class object. Should be > x = myClass() > > > x.myMethod() > > then this will work. Ah! Thank you! I knew/guessed the alias bit from my experience with C++, but I couldn't figure out exactly what I needed. I've seen the "self" reference before, but I never really understood it. Let me see if I understand correctly: class myClass(): creates a new "data type"(?), called myClass, and x = myClass() creates a variable with the type of "myClass", similar to foo = dict() and the method inside myClass defined as myMethod(self), can be called on the data type "myClass" like this: x.myMethod() is that fairly accurate? -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expressions...
On Fri, May 2, 2008 at 1:38 PM, Spencer Parker <[EMAIL PROTECTED]> wrote: > Well...it gives me the entire path...I am not running this script from the > actual directory...I am running it from a secure user directory that only > has certain access rights. During the os.path.dirname gives me the entire > directory path...I just need to last part of it is all. out of > '/xen/domains2/machinename/disk.img all I need is 'machinename' In [1]: import os In [2]: path = '/xen/domains2/machinename/disk.img' In [3]: os.path.basename(os.path.dirname(path)) Out[3]: 'machinename' Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expressions...
dir = '/xen/domains2/machinename/disk.img' a = dir.split('/')[3] is what I would use... On Fri, May 2, 2008 at 12:38 PM, Spencer Parker <[EMAIL PROTECTED]> wrote: > Well...it gives me the entire path...I am not running this script from the > actual directory...I am running it from a secure user directory that only > has certain access rights. During the os.path.dirname gives me the entire > directory path...I just need to last part of it is all. out of > '/xen/domains2/machinename/disk.img all I need is 'machinename' > > On Fri, May 2, 2008 at 10:23 AM, Kent Johnson <[EMAIL PROTECTED]> wrote: > > > On Fri, May 2, 2008 at 12:08 PM, Spencer Parker <[EMAIL PROTECTED]> > > wrote: > > > I need to use a regular expression to get a couple of items for my > > python > > > script. So far the script is running an 'ls' command to get a few > > items > > > that I need > > > > > > I run an 'ls -sk /xen/domains2/machinename/disk.img' > > > > > > Output > > > > > > 2454112 /xen/domains2/machinename/disk.img > > > > Look at os.path.getsize(). You don't need to use ls to get this > > information. > > > > > What I need to get is just the size numbers using regular expressions. > > > > You could just use str.split() but getsize() is better. > > > > > I also need to get the 'machinename' > > > portion of the path as well to stick into the database. > > > > Don't you already know this? Where does the ls command come from? > > Anyway look at os.path.split() and os.path.dirname(). > > > > Kent > > > > > > -- > Spencer Parker > ___ > > "if you can't go to heaven, may you at least die in Ireland." > > ___ > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expressions...
Well...it gives me the entire path...I am not running this script from the actual directory...I am running it from a secure user directory that only has certain access rights. During the os.path.dirname gives me the entire directory path...I just need to last part of it is all. out of '/xen/domains2/machinename/disk.img all I need is 'machinename' On Fri, May 2, 2008 at 10:23 AM, Kent Johnson <[EMAIL PROTECTED]> wrote: > On Fri, May 2, 2008 at 12:08 PM, Spencer Parker <[EMAIL PROTECTED]> > wrote: > > I need to use a regular expression to get a couple of items for my python > > script. So far the script is running an 'ls' command to get a few items > > that I need > > > > I run an 'ls -sk /xen/domains2/machinename/disk.img' > > > > Output > > > > 2454112 /xen/domains2/machinename/disk.img > > Look at os.path.getsize(). You don't need to use ls to get this > information. > > > What I need to get is just the size numbers using regular expressions. > > You could just use str.split() but getsize() is better. > > > I also need to get the 'machinename' > > portion of the path as well to stick into the database. > > Don't you already know this? Where does the ls command come from? > Anyway look at os.path.split() and os.path.dirname(). > > Kent > -- Spencer Parker ___ "if you can't go to heaven, may you at least die in Ireland." ___ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Method question?
On Fri, May 2, 2008 at 12:55 PM, W W <[EMAIL PROTECTED]> wrote: > I'm playing around with pyGTK, and I have a question to see if I'm > understanding things correctly. > > Is a method just a function inside a class? Pretty much. It also has a required 'self' parameter which gets the value of the object on which the method is called. > > i.e. > > def myFunction(): > print "This is a function" > > class myClass: > def myMethod(): should be def myMethod(self): > print "This is a method" > > and to call: > > myFunction() > > I tried to call the method via. myClass.myMethod() and > x = myClass This just makes x an alias for the class object. Should be x = myClass() > x.myMethod() then this will work. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Method question?
I'm playing around with pyGTK, and I have a question to see if I'm understanding things correctly. Is a method just a function inside a class? i.e. def myFunction(): print "This is a function" class myClass: def myMethod(): print "This is a method" and to call: myFunction() I tried to call the method via. myClass.myMethod() and x = myClass x.myMethod() but none of those were working... I'll take a look online, but am I correct that the only difference is that method is a function inside a class? Thanks, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expressions...
On Fri, May 2, 2008 at 12:08 PM, Spencer Parker <[EMAIL PROTECTED]> wrote: > I need to use a regular expression to get a couple of items for my python > script. So far the script is running an 'ls' command to get a few items > that I need > > I run an 'ls -sk /xen/domains2/machinename/disk.img' > > Output > > 2454112 /xen/domains2/machinename/disk.img Look at os.path.getsize(). You don't need to use ls to get this information. > What I need to get is just the size numbers using regular expressions. You could just use str.split() but getsize() is better. > I also need to get the 'machinename' > portion of the path as well to stick into the database. Don't you already know this? Where does the ls command come from? Anyway look at os.path.split() and os.path.dirname(). Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python -v command
Thank you to everyone for your help. I'm sorry to waste your time with such a silly question. I am now back up and running! I really appreciate your responses. On Fri, May 2, 2008 at 11:16 AM, Kent Johnson <[EMAIL PROTECTED]> wrote: > On Fri, May 2, 2008 at 11:33 AM, Stephanie <[EMAIL PROTECTED]> > wrote: > > Hi, > > I'm at a very beginning level of Python knowledge, but I use several > Python > > programs via the Macintosh Unix Terminal. I was trying to quickly see > which > > version of Python I am running and typed in the command "python -v". > That > > was obviously not the correct command to use. It installed several > things > > and now I'm unable to run my Python programs. > > > > Can someone tell me if there there is a way to undo the python -v > command? > > You have started python in verbose mode. It didn't actually install > anything, it is showing you all the modules that are loaded when > python starts up. > > At the end, notice that the prompt is >>>. That is the python > interpreter prompt. Type control-D to exit the interpreter and get > back to the shell prompt. > > Near the end, notice that it did also print the version - Python > always does that when it runs interactively. You can also use the > command > python --version > to print the version number and exit. > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Regular expressions...
On Fri, May 2, 2008 at 12:08 PM, Spencer Parker <[EMAIL PROTECTED]> wrote: > I need to use a regular expression to get a couple of items for my python > script. So far the script is running an 'ls' command to get a few items > that I need Why do you need to use regular expressions? This problem doesn't really seem to call for them. You should be able to just use the split method of strings. Here's an example: IDLE 1.2.2 >>> command1 = "2454112 /xen/domains2/machinename/disk.img" >>> command2 = "-rw-r--r-- 1 root root 20980736 May 2 10:05 /xen/domains2/machinename/disk.img" >>> command1.split() ['2454112', '/xen/domains2/machinename/disk.img'] >>> command2.split() ['-rw-r--r--', '1', 'root', 'root', '20980736', 'May', '2', '10:05', '/xen/domains2/machinename/disk.img'] >>> bytes_used = command1.split()[0] >>> bytes_actual = command2.split()[4] >>> print bytes_used 2454112 >>> print bytes_actual 20980736 >>> percent = float(bytes_used) / float(bytes_actual) >>> percent 0.11696977646542046 >>> -- Jerry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python -v command
On Fri, May 2, 2008 at 11:33 AM, Stephanie <[EMAIL PROTECTED]> wrote: > Hi, > I'm at a very beginning level of Python knowledge, but I use several Python > programs via the Macintosh Unix Terminal. I was trying to quickly see which > version of Python I am running and typed in the command "python -v". That > was obviously not the correct command to use. It installed several things > and now I'm unable to run my Python programs. > > Can someone tell me if there there is a way to undo the python -v command? You have started python in verbose mode. It didn't actually install anything, it is showing you all the modules that are loaded when python starts up. At the end, notice that the prompt is >>>. That is the python interpreter prompt. Type control-D to exit the interpreter and get back to the shell prompt. Near the end, notice that it did also print the version - Python always does that when it runs interactively. You can also use the command python --version to print the version number and exit. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
On Fri, May 2, 2008 at 11:42 AM, Paul McGuire <[EMAIL PROTECTED]> wrote: > f=open('file.txt',r) > print_line = False > for line in f: >if print_line: > print line > print_line = False >if line == "3": Don't forget about the newline...(that makes four!) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python -v command
On Fri, May 2, 2008 at 11:33 AM, Stephanie <[EMAIL PROTECTED]> wrote: > Hi, > I'm at a very beginning level of Python knowledge, but I use several Python > programs via the Macintosh Unix Terminal. I was trying to quickly see which > version of Python I am running and typed in the command "python -v". That > was obviously not the correct command to use. It installed several things > and now I'm unable to run my Python programs. > > Can someone tell me if there there is a way to undo the python -v command? > Any suggestions for how I can remedy the situation. 'python -v' just runs the python interpreter in verbose mode -- all those messages are about various libraries the interactive interpreter loads before it gives you the prompt. Running 'python -v' didn't (or at least should not) change anything in your environment. Can you show us what happens when you try to run some of your existing code that used to work? -- Jerry ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Regular expressions...
I need to use a regular expression to get a couple of items for my python script. So far the script is running an 'ls' command to get a few items that I need I run an 'ls -sk /xen/domains2/machinename/disk.img' Output 2454112 /xen/domains2/machinename/disk.img Then I have it running an 'ls -lk /xen/domains2/machinename/disk.img output: -rw-r--r-- 1 root root 20980736 May 2 10:05 /xen/domains2/machinename/disk.img Then I take those two number and divide them using the the used amount by the actual size of the disk to get a percentage. I take all of that data and then stick it into a database. What I need to get is just the size numbers using regular expressions. I tried using an index, but since the disk images can up to 20 gig and be as low as 5 gig...the index changes. I also need to get the 'machinename' portion of the path as well to stick into the database. -- Spencer Parker ___ "if you can't go to heaven, may you at least die in Ireland." ___ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] python -v command
Hi, I'm at a very beginning level of Python knowledge, but I use several Python programs via the Macintosh Unix Terminal. I was trying to quickly see which version of Python I am running and typed in the command "python -v". That was obviously not the correct command to use. It installed several things and now I'm unable to run my Python programs. Can someone tell me if there there is a way to undo the python -v command? Any suggestions for how I can remedy the situation. I'm ready to crawl into a dark hole and hide - I feel very foolish! Thanks so much for putting up with a newbie's silly question. Here's the Terminal output in case that helps at all: admin$ python -v # installing zipimport hook import zipimport # builtin # installed zipimport hook # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site.py import site # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/site.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/os.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/os.py import os # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/os.pyc import posix # builtin # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/posixpath.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/posixpath.py import posixpath # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/posixpath.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/stat.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/stat.py import stat # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/stat.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/UserDict.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/UserDict.py import UserDict # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/UserDict.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy_reg.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy_reg.py import copy_reg # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/copy_reg.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/types.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/types.py import types # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/types.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/warnings.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/warnings.py import warnings # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/warnings.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/linecache.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/linecache.py import linecache # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/linecache.pyc import encodings # directory /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/__init__.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/__init__.py import encodings # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/__init__.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/codecs.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/codecs.py import codecs # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/codecs.pyc import _codecs # builtin # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/aliases.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/aliases.py import encodings.aliases # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/aliases.pyc # /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/ascii.pyc matches /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/ascii.py import encodings.ascii # precompiled from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/encodings/ascii.pyc Python 2.4.4 (#1, Oct 18 2006, 10:34:39) [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin Type "help", "copyright", "credits" or "license" for more information. import readline # dynamically loaded from /Library/Frameworks/Python.framework/Versions/2.4/lib/python2.4/lib-dynload/readline.so >>> ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
Augghh! I can't stand it!!! If position is a boolean, then *why* must we test if it is equal to True?!!! It's a boolean! Just test it! For that matter, let's rename "position" to something a little more direct, "print_line" perhaps? Did you know that files are now iterators? If going through the file line by line, no need for read().split(), or readlines(), or anything else, just iterate over f: f=open('file.txt',r) print_line = False for line in f: if print_line: print line print_line = False if line == "3": print_line = True f.close() Is there only one line containing a "3" in this file? What if that were the 3rd line in a file containing 50 zillion lines? After printing the line after "3", this program iterates through the whole rest of the file, for nothing. Nothing! If you only want the first (or only) line after the line containing "3", then do: f=open('file.txt',r) print_line = False for line in f: if print_line: print line # found it, no need to read any further break if line == "3": print_line = True f.close() -- Paul ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] best way to get external data
On Fri, May 2, 2008 at 11:12 AM, Bryan Fodness <[EMAIL PROTECTED]> wrote: > I am trying to figure out the best way to get external data. 1. Put the data in a Python module and import it 2. Put the data in a .ini file and read it with the ConfigParser module http://docs.python.org/lib/module-ConfigParser.html > If I have a long list of 'variable' = 'value', could this be better managed > in a dictionary? Possibly. When you have a list of variables you are trying to create by name, a dictionary is definitely suggested. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
On Fri, May 2, 2008 at 10:34 AM, Brain Stormer <[EMAIL PROTECTED]> wrote: > f=open('file.txt',r) > position =False > > for line in f.read().split(): Note that split() splits on any whitespace, not just line endings. In your case it doesn't much matter I guess. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] best way to get external data
I am trying to figure out the best way to get external data. Using the following data in a file 1 2 3 I have used, fi = open(infile, 'r') s = fi.readlines() fi.close() a = s[0] b = s[1] c = s[2] but, if I have, x = 1 y = 2 z = 3 I have used, for line in open(infile): i_line = line.split() if i_line[0] == 'x': a = i_line[2] elif i_line[0] == 'y': b = i_line[2] elif i_line[0] == 'z': c = i_line[2] I do have control over the creation of the data file. The second way is better in my mind, because it is independent of the ordering. If I have a long list of 'variable' = 'value', could this be better managed in a dictionary? Or, is there a better way? Thanks -- "The game of science can accurately be described as a never-ending insult to human intelligence." - João Magueijo ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
f=open('file.txt',r) position =False for line in f.read().split(): if position == True print line position = False if line == "3": position = True else: position = False f.close() On Fri, May 2, 2008 at 10:28 AM, Brain Stormer <[EMAIL PROTECTED]> wrote: > You are correct. It is missing the ":" and it will print "3" > > On Fri, May 2, 2008 at 10:18 AM, bob gailer <[EMAIL PROTECTED]> wrote: > > > Brain Stormer wrote: > > > > > Well, > > > I was somewhat confused with all of the answers so I decided to go > > > with my/following method. Kent's method has 4 fewer lines of code than > > > mine and cleaner. Please correct me if I am fundamentally wrong. > > > > > > f=open('file.txt',r) > > > > > > for line in f.read().split(): > > >if line == "3" > > > position = True > > >else: > > > position = False > > >if position == True > > > print line > > > position = False > > > f.close() > > > > > > Yikes! That won't compile (missing : at end of if statements). After > > correcting, will print 3. > > > > How about this if you want less lines of code: > > > > f = open('file.txt',r).readlines() > > print f[[x+1 for x,line in enumerate(f) if line.rstrip() == "3"][0]] > > > > > > -- > > Bob Gailer > > 919-636-4239 Chapel Hill, NC > > > > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
You are correct. It is missing the ":" and it will print "3" On Fri, May 2, 2008 at 10:18 AM, bob gailer <[EMAIL PROTECTED]> wrote: > Brain Stormer wrote: > > > Well, > > I was somewhat confused with all of the answers so I decided to go with > > my/following method. Kent's method has 4 fewer lines of code than mine and > > cleaner. Please correct me if I am fundamentally wrong. > > > > f=open('file.txt',r) > > > > for line in f.read().split(): > >if line == "3" > > position = True > >else: > > position = False > >if position == True > > print line > > position = False > > f.close() > > > > Yikes! That won't compile (missing : at end of if statements). After > correcting, will print 3. > > How about this if you want less lines of code: > > f = open('file.txt',r).readlines() > print f[[x+1 for x,line in enumerate(f) if line.rstrip() == "3"][0]] > > > -- > Bob Gailer > 919-636-4239 Chapel Hill, NC > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
Brain Stormer wrote: Well, I was somewhat confused with all of the answers so I decided to go with my/following method. Kent's method has 4 fewer lines of code than mine and cleaner. Please correct me if I am fundamentally wrong. f=open('file.txt',r) for line in f.read().split(): if line == "3" position = True else: position = False if position == True print line position = False f.close() Yikes! That won't compile (missing : at end of if statements). After correcting, will print 3. How about this if you want less lines of code: f = open('file.txt',r).readlines() print f[[x+1 for x,line in enumerate(f) if line.rstrip() == "3"][0]] -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
Well, I was somewhat confused with all of the answers so I decided to go with my/following method. Kent's method has 4 fewer lines of code than mine and cleaner. Please correct me if I am fundamentally wrong. f=open('file.txt',r) for line in f.read().split(): if line == "3" position = True else: position = False if position == True print line position = False f.close() On Fri, May 2, 2008 at 6:55 AM, Kent Johnson <[EMAIL PROTECTED]> wrote: > On Fri, May 2, 2008 at 3:55 AM, Roel Schroeven > <[EMAIL PROTECTED]> wrote: > > > Shouldn't it even be 'line = f.next()'? > > Wow, I think Brain Stormer should get a prize. I'm not sure what the > prize is, but his short program has elicited incomplete and inaccurate > answers from three of the top posters to this list! I *think* this is > a complete answer: > > f = open('file.txt',r) > for line in f: > if line.rstrip() == "3": > line = f.next() > print line > break # put this in if there is only one line you want to print > f.close() > > ...awaiting any further corrections :-) > Kent > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] put? and files
On Thu, May 1, 2008 at 11:42 PM, Ross Glover <[EMAIL PROTECTED]> wrote: > Thanks all for your in[put]. It did take me a minute to figure out that 2 > variables could get assigned using this method. Thanks to your help, I > managed to cobble together a fully functional GUI based dictionary program > (MY FIRST!). > That said, it would seem that I do need to understand files better. Does > anyone have a suggestion for a solid and detailed explanation? Either > online or a book? I don't think you need to know more about files than you will get in any introductory book. The files chapter of Beginning Python should be fine. > What I want to do is write a program that parses text based on tags that I > create, writes the text into separate files based on specified tags, i.e. > each tag gets its own file. At least this is my starting goal. Text parsing varies widely. At the simple end, you can use string methods such as split(), strip() and slicing to extract the text you want. See the strings chapter of BP or http://docs.python.org/lib/string-methods.html The next level of complexity uses regular expressions to extract text. There is a bit of a learning curve to this, but regexes are a very powerful and useful tool that you can use outside of Python as well. They are helpful when your parser must recognize specific patterns. http://www.amk.ca/python/howto/regex/ For more complicated formats you will want a specialized parser. For example Python includes a parser for XML; BeautifulSoup is a popular third-party parser for HTML; pyparsing is a fairly easy-to-use library for building specialized parsers. You will want a specialized parser if your text format has many variations or if it allows nested patterns. If you give some examples of what you want to parse we can give more specific advice. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
On Fri, May 2, 2008 at 3:55 AM, Roel Schroeven <[EMAIL PROTECTED]> wrote: > Shouldn't it even be 'line = f.next()'? Wow, I think Brain Stormer should get a prize. I'm not sure what the prize is, but his short program has elicited incomplete and inaccurate answers from three of the top posters to this list! I *think* this is a complete answer: f = open('file.txt',r) for line in f: if line.rstrip() == "3": line = f.next() print line break # put this in if there is only one line you want to print f.close() ...awaiting any further corrections :-) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] put? and files
"Ross Glover" <[EMAIL PROTECTED]> wrote That said, it would seem that I do need to understand files better. Does anyone have a suggestion for a solid and detailed explanation? Either online or a book? Reading the Python tutorial section might be enough. If you understand the books you mention you should find the official tutorial easy enough. Or try my tutorial topic first for the basics. I've been working through the book Beginning Python, and I also went through AByteOfPython. Slowly it's beginning to make sense, but I would very much like to know what my next best step should be given the above mentioned goal. Text processing still eludes my understanding. For text processing try the online version of David Mertz' book "Text Processing in Python" - try a Google search, I think its at gnossis.com or somesuch. The only caveat is that he is a fan of functional programming which can seem a tad strange if you aren't used to it. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to skip to next line in for loop
Alan Gauld schreef: if line == "3": line.next this then becomes f.next() # next is a method not an attribute so needs the () to call it Shouldn't it even be 'line = f.next()'? -- The saddest aspect of life right now is that science gathers knowledge faster than society gathers wisdom. -- Isaac Asimov Roel Schroeven ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor