Re: [Tutor] Formatting output into columns
Luke Paireepinart wrote: Scott Oertel wrote: Someone asked me this question the other day, and I couldn't think of any easy way of printing the output besides what I came up with pasted below. So what you have is a file with words in it as such: apple john bean joke ample python nice and you want to sort and output the text into columns as such: a p j b n apple python john bean nice ample joke and this is what works, but I would also like to know how to wrap the columns, plus any ideas on a better way to accomplish this. #!/usr/bin/env python data = {} lrgColumn = 0 for line in open(test.txt,r).read().splitlines(): you can just directly do for line in open('test.txt'): depending on your Python version. I believe it's 2.3+. I have 2.4 and it works there, at least. If using an older version of Python, you can use .readlines() instead of .read().splitlines() I believe Kent and Alan already helped you with your original question. -Luke The reason I use read().splitlines, is because if you do .readlines() it adds the carriage return to the end of each line where in i have to .rstrip() to remove it. If you use .read() it doesn't split the lines in the file into a tuple, there for you it is not an iteration. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A replacement for a for loop
Terry Carroll wrote: On Wed, 29 Aug 2007, Scott Oertel wrote: Why even have the keys variable at all.. for key in attrs: print 'Attribute %s has value %s' % (key, attrs[key]) In a prior email thread, the OP indicated that he needed to process the keys in that particular order; and it's not really amenable to any sort. Yup, I didn't see that the first time, sorry. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Formatting output into columns
Someone asked me this question the other day, and I couldn't think of any easy way of printing the output besides what I came up with pasted below. So what you have is a file with words in it as such: apple john bean joke ample python nice and you want to sort and output the text into columns as such: a p j b n apple python john bean nice ample joke and this is what works, but I would also like to know how to wrap the columns, plus any ideas on a better way to accomplish this. #!/usr/bin/env python data = {} lrgColumn = 0 for line in open(test.txt,r).read().splitlines(): char = line[0].lower() if not char in data: data[char] = [line] else: data[char].append(line) for item in data: print item.ljust(10), if len(data[item]) lrgColumn: lrgColumn = len(data[item]) print for item in range(lrgColumn): for i in data.iteritems(): try: print i[1][item].ljust(10), except IndexError: print .ljust(10), print ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Formatting output into columns
Alan Gauld wrote: Scott Oertel [EMAIL PROTECTED] wrote and you want to sort and output the text into columns as such: a p j b n apple python john bean nice ample joke and this is what works, but I would also like to know how to wrap the columns, plus any ideas on a better way to accomplish this. Use format strings. You can calculate the column widths by analyzing the data then create a format string for the required number of columns. Finally insert the data on each row from a tuple. HTH, Do you have any good documentation that could shed some more light on exactly how to use format strings in such a way? -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] A replacement for a for loop
John Fouhy wrote: On 29/08/07, Trey Keown [EMAIL PROTECTED] wrote: attrs={u'title': u'example window title', u'name': u'SELF', u'icon': u'e.ico'} keys = ['name','title','icon'] for (tag, val) in attrs.iteritems(): for key in keys: print val the first for tag causes the dictionary (attrs) to have its keys called tag and its value called val. The second for loop causes the dictionary keys to be read in a certain order. How could I take away the first for loop and replace it with something else to do the same general function? for key in keys: print 'Attribute %s has value %s' % (key, attrs[key]) Why even have the keys variable at all.. for key in attrs: print 'Attribute %s has value %s' % (key, attrs[key]) -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] #!/usr/bin/env python vs #!/usr/local/bin/python
[EMAIL PROTECTED] wrote: hi list, how to choose between #!/usr/bin/env python and #!/usr/local/bin/python in the beginning of the script ? e. - SCENA - Ĺäčíńňâĺíîňî ÁĹÇĎËŔŇÍÎ ńďčńŕíčĺ çŕ ěîáčëíč ęîěóíčęŕöčč č ňĺőíîëîăčč. http://www.bgscena.com/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor One of the main dis-advantages to using '/usr/bin/env python' is that you have to either A) make sure the environment is initialized, or B) initialize the environment manually before executing the script. If you, for example, want to use a python script at boot time, before the environment is initialized, i strongly recommend using an absolute path. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] code improvement for beginner ?
lmac wrote: --- The problem with downloading the images is this: - http://images.nfl.com/images/globalnav-shadow-gray.gif Traceback (most recent call last): File /home/internet/bin/nflgrab.py, line 167, in ? urllib.urlretrieve(img,img[f:]) File /usr/lib/python2.3/urllib.py, line 83, in urlretrieve return _urlopener.retrieve(url, filename, reporthook, data) File /usr/lib/python2.3/urllib.py, line 216, in retrieve tfp = open(filename, 'wb') IOError: [Errno 13] Permission denied: '/globalnav-shadow-gray.gif' - Is there any solution to know if i can download the image ? Thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor I used this in my previous program while having problems writing files. from sys import argv from os.path import dirname, join try: tfp = open(join(dirname(argv[0]), filename), 'wb') except IOError: print Unable to write file, pick another directory this will save it into the directory that your program resides in. you might want to check out this current lib doc, http://www.python.org/doc/current/lib/os-file-dir.html it has a bunch of objects of the os module that are good for testing if a directory is writable ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] CREATING A PR.EXE FROM A PR.PY
Jack Anema wrote: Can you or someone give me very specific instructions as to howto create an _.EXE file from a _.PY file. I have spent a lot of time looking through all 7 sections of FAQ's at http://www.python.org/doc/faq/ and many other sights. I have also gone to http://starship.python.net/crew/theller/py2exe/ and tried to use the general comments there. I did download and install *win-py2.4.exe . This installed successfully as far as I can see. There were no error messages and REMOVEPY2EXE.EXE was created in my PYTHON24 folder. The machine I am running, is running XP professional, 2002. It has a Pentiuum(R) 4, 1.8 Ghz CPU. The drive has 37 GB free. I am by no means a computer expert but I am not illiterate either. I have used MS Quick Basic for many years. In the past I have also used APL and Fortran on various machines over the years. I just am not familiar enough with Python and have not been able to find the detailed data I need to do what I want. Up to a month ago I had never heard of Python and I badly need the ability to make *.PY programs filesrun as *.EXE files. Can you or someone supply the very specific instructions to do this? I would also be perfectly willing to arrange with someone to call them at their convenience and do it over the phone and compensate the person for their effort and time. I sincerely and very much appreciate anyone's response and help! Thank you! Jack Anema. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Try using py2exe it works pretty well for compiling .py into a portable .exe distribution. http://starship.python.net/crew/theller/py2exe/ -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] mod_python.publisher
I'm having an issue with mod_python.publisher, supposedly i should be able to just place this code def index(): return "This is only a test." into test.py and when placed into my browser it should run the index function by default, but instead i get a 404 error. i'm running Apache/1.3.33 (Unix) mod_python/2.7.11 Python/2.2.3 -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] mod_python.publisher
Scott Oertel wrote: I'm having an issue with mod_python.publisher, supposedly i should be able to just place this code def index(): return "This is only a test." into test.py and when placed into my browser it should run the index function by default, but instead i get a 404 error. i'm running Apache/1.3.33 (Unix) mod_python/2.7.11 Python/2.2.3 -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Just for fun i decided to upgrade the python version to 2.4.1 Apache/1.3.33 (Unix) mod_python/2.7.11 Python/2.4.1 mod_gzip/1.3.26.1a mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_ssl/2.8.22 OpenSSL/0.9.7a PHP-CGI/0.1b still no luck =( ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
Bob Gailer wrote: At 02:55 PM 8/24/2005, Scott Oertel wrote: How do I use the built in file objects to insert text into a file at a certain location? i.e. something, 2, chance, weee nothing, happened, crap, nice need to search for something and insert, what, before it Here's the algorithm. If you know enough Python you will be able to code it. So put together a program, give it a try and come back with questions. read the file into a string variable (assuming the file is not humungus) find the location of something in the string assemble a new string consisting of: the original string up to the location (index) of something what the rest of the original string write the new string to the file Bob Gailer 303 442 2625 home 720 938 2625 cell basically I took the idea and the code example given and wrote this little function, i stuck vars in this html page like #email# and just used it like this, insertdata('#email#','[EMAIL PROTECTED]') works perfect! def insertdata(name, data): file = open('template.html', 'r+') contents = file.read() pos = contents.index(name) file.seek(pos) file.write(data) file.write(contents[pos + len(name):]) file.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Counting help
Kent Johnson wrote: Scott Oertel wrote: The next problem I have though is creating the dict, i have a loop, but i can't figure out how to compile the dict, it is returning this: ('Joey Gale', ('Scott Joe', 'This is lame' ))) listofnames = [] while (cnt number[1][0]): if (date[2] == today[2]): test = regex.findall(M.fetch(int(number[1][0]) - cnt, '(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip()) cnt += 1 if (nameofsender != []): print nameofsender[0] listofnames = nameofsender[0], listofnames I think you want listofnames.append(nameofsender[0]) which will add nameofsender[0] to the list. What you have - listofnames = nameofsender[0], listofnames is making a tuple (a pair) out of the new name and the old list, and assigning it to listofnames. Kind of like CONS in LISP - but Python lists are more like arrays than like LISP lists. Kent else: no_name += 1 else: break ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thank you everyone, this is exactly it, I'm going to take that link from byron and read up on dicts/lists. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Working with files
How do I use the built in file objects to insert text into a file at a certain location? i.e. something, 2, chance, weee nothing, happened, crap, nice need to search for something and insert, what, before it thanks for the feedback you guys are great :) -Scott Oertel -Py2.4 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Working with files
Byron wrote: Bob Gailer wrote: read the file into a string variable (assuming the file is not humungus) find the location of "something" in the string assemble a new string consisting of: the original string up to the location (index) of "something" "what" the rest of the original string write the new string to the file Hi Scott, Bob gave you the basic instructions that you need to complete the task that you are asking for. If you don't know how to do this, I would suggest that you start with the following Python tutorial: http://www.greenteapress.com They provide an excellent tutorial for learning the basics of Python -- and best of all, it's free and written for absolute beginners. Take care, Byron --- ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Thanks again, I figured it out with the index function of the file objects, I didn't know that you could search for a word and find the exact pos of it, that was the tiny bit of information I was looking for. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Counting help
I have extracted a list of names, i.e. Joe Smith Joe Smith Jack Smith Sam Love Joe Smith I need to be able to count the occurances of these names and I really don't have any idea where to begin. Any ideas? excuse me this is my first post to this list, I hope I included enough information. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Counting help
Byron wrote: Luis N wrote: Ideally, you would put your names into a list or dictionary to make working with them easier. If all you're trying to do is count them (and your list of names is long), you might consider a dictionary which you would use like so: #This is just the first thing I considered. l = ['a list of names'] d = {} for name in namelist: if d.has_key(name): x = d.get(name) d[name] = x + 1 else: d[name] = 1 100% agreed. I have used this approach before and it works great... Byron Thanks for the snipplet, it's perfect for what I'm doing, I wasn't aware of the has_key() or get(), this is very usefull. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Counting help
Scott Oertel wrote: Byron wrote: Luis N wrote: Ideally, you would put your names into a list or dictionary to make working with them easier. If all you're trying to do is count them (and your list of names is long), you might consider a dictionary which you would use like so: #This is just the first thing I considered. l = ['a list of names'] d = {} for name in namelist: if d.has_key(name): x = d.get(name) d[name] = x + 1 else: d[name] = 1 100% agreed. I have used this approach before and it works great... Byron Thanks for the snipplet, it's perfect for what I'm doing, I wasn't aware of the has_key() or get(), this is very usefull. -Scott Oertel ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor The next problem I have though is creating the dict, i have a loop, but i can't figure out how to compile the dict, it is returning this: ('Joey Gale', ('Scott Joe', ('This is lame' ))) listofnames = [] while (cnt number[1][0]): if (date[2] == today[2]): test = regex.findall(M.fetch(int(number[1][0]) - cnt, '(BODY[HEADER.FIELDS (FROM)])')[1][0][1].rstrip()) cnt += 1 if (nameofsender != []): print nameofsender[0] listofnames = nameofsender[0], listofnames else: no_name += 1 else: break ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor