Re: [Tutor] Multiple regex replacements, lists and for.
> I'm new to python and inexperienced in programming but I'm trying hard. > I have a shell script that I'm converting over to python. > Part of the script replaces some lines of text. > I can do this in python, and get the output I want, but so far only using sed. > Here's an example script: > > import subprocess, re > list = ['Apples the color red', 'Skyi am the blue color', 'Grass > the > colour green is here', 'Sky i am the blue color'] > > def oldway(): > sed_replacements = """ > s/\(^\w*\).*red/\\1:RED/ > s/\(^\w*\).*blue.*/\\1:BLUE/""" > sed = subprocess.Popen(['sed', sed_replacements], > stdin=subprocess.PIPE, stdout=subprocess.PIPE) > data = sed.communicate("\n".join(list))[:-1] > for x in data: > print x > oldway(); > > """ This produces: > Apples:RED Sky:BLUE Grass the colour green is here Sky:BLUE > > Which is what I want""" > > print "---" > > def withoutsed(): > replacements = [ > (r'.*red', 'RED'), > (r'.*blue.*', 'BLUE')] > for z in list: > for x,y in replacements: > if re.match(x, z): > print re.sub(x,y,z) > break > else: > print z > withoutsed(); > > """ Produces: > RED Skyi am the blue color BLUE Grass the colour green is here Grass the colour green is here Skyi am the blue color BLUE > > Duplicate printing + other mess = I went wrong""" > > I understand that it's doing what I tell it to, and that my for and if > statements are wrong. You should make your Python regex more like sed. re.sub() always returns a string, either changed or unchanged. So you can "pipe" the two necessary re.sub() onto each other, like you do for sed: re.sub(replacement, replacement, re.sub(replacement, replacement, string). That removes the inner for loop, because you can do all the replacements in one go. re.sub() will return the original string if there was no replacement (again like sed), so you can remove the if-statement with the re.match: re.sub() will leave the 'Grass' sentence untouched, but still print it. Lastly, in your sed expression, you're catching the first non-whitespace characters and substitute them in the replacements, but you don't do in re.sub(). Again, this is practically the same format, the only difference being that in Python regexes, you don't need to escape the grouping parentheses. I can give you the full solution, but I hope this is pointer in the right direction is good enough. All in all, your code can be as efficient in Python as in sed. Cheers, Evert Oh, btw: semi-colons at the end of a statement in Python are allowed, but redundant, and look kind of, well, wrong. > What I want to do is replace matching lines and print them, and also > print the non-matching lines. > Can somebody please point me in the right direction? > > Any other python pointers or help much appreciated, > > Will. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] list of dict question
On 11/10/2010 19.23, Alan Gauld wrote: ... HTH, Sure it did! Very enlightening, Alan. THANK YOU! Nessun virus nel messaggio in uscita. Controllato da AVG - www.avg.com Versione: 9.0.862 / Database dei virus: 271.1.1/3190 - Data di rilascio: 10/11/10 08:34:00 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] SQLite3 DB Field Alphabetizing
When calling a sqlite3 db file in python 2.6 on Ubuntu, I get the following when the items are taken from the db tuple, lstripped('u'), and added to a list. ['.hjvkjgfkj/bdgfkjbg', 'uuz', 'jgkgyckghc', 'kfhhv ', 'khfhf', 'test', 'test10', 'test2', 'test3', 'test346w43', 'test4', 'test5', 'test6', 'test7', 'test7uyuy', 'test8', 'test9', 'testgraph', ';s;juf;sfkh', 'zzrerhshhjrs'] My question is, why is everything except [:-2] in alphabetical order? It doesn't really matter(at this point), for my purposes, but I'd like to know when they changed the abc's to xy;z's? Thanks, David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
On 10/12/2010 7:41 AM, David Hutto wrote: When calling a sqlite3 db file Calling? How did you do that? I presume a sql select statement. True? If so please post the statement. Else what do you mean by call? in python 2.6 on Ubuntu, I get the following when the items are taken from the db tuple, lstripped('u'), and added to a list. ['.hjvkjgfkj/bdgfkjbg', 'uuz', 'jgkgyckghc', 'kfhhv ', 'khfhf', 'test', 'test10', 'test2', 'test3', 'test346w43', 'test4', 'test5', 'test6', 'test7', 'test7uyuy', 'test8', 'test9', 'testgraph', ';s;juf;sfkh', 'zzrerhshhjrs'] My question is, why is everything except [:-2] in alphabetical order? It doesn't really matter(at this point), for my purposes, but I'd like to know when they changed the abc's to xy;z's? -- Bob Gailer 919-636-4239 Chapel Hill NC ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] urllib problem
Hoi, I have this programm : import urllib import re f = urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6";) inhoud = f.read() f.close() nummer = re.search('[0-9]', inhoud) volgende = int(nummer.group()) teller = 1 while teller <= 3 : url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="; + str(volgende) f = urllib.urlopen(url) inhoud = f.read() f.close() nummer = re.search('[0-9]', inhoud) print "nummer is", nummer.group() volgende = int(nummer.group()) print volgende teller = teller + 1 but now the url changes but volgende not. What do I have done wrong ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
On Tue, 12 Oct 2010 10:41:36 pm David Hutto wrote: > When calling a sqlite3 db file in python 2.6 on Ubuntu, I get the > following when the items are taken from the db tuple, lstripped('u'), > and added to a list. > > ['.hjvkjgfkj/bdgfkjbg', 'uuz', 'jgkgyckghc', > 'kfhhv ', 'khfhf', 'test', 'test10', 'test2', 'test3', 'test346w43', > 'test4', 'test5', 'test6', 'test7', 'test7uyuy', 'test8', 'test9', > 'testgraph', ';s;juf;sfkh', 'zzrerhshhjrs'] > > My question is, why is everything except [:-2] in alphabetical order? *shrug* Perhaps they were added to the DB in alphabetical order, except for the entry starting with a semi-colon? Who supplied the database and how did they insert the data? Perhaps it's some artifact of whatever internal optimizations the DB uses? I expect the DB table is something like a hash table, only more complicated, so it's not impossible that the hashing function used ends up storing items in almost-but-not-quite alphabetical order. Maybe it's just a fluke. They have to be in *some* order. Since you haven't told us how you added them to the list, perhaps you deliberately added them in that order and are trying to trick us. In which case, HA! I see through your cunning trick! *wink* -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] urllib problem
> I have this program : > > import urllib > import re > f = > urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6";) > inhoud = f.read() > f.close() > nummer = re.search('[0-9]', inhoud) > volgende = int(nummer.group()) > teller = 1 > while teller <= 3 : > url = "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="; + > str(volgende) > f = urllib.urlopen(url) > inhoud = f.read() > f.close() > nummer = re.search('[0-9]', inhoud) > print "nummer is", nummer.group() > volgende = int(nummer.group()) > print volgende > teller = teller + 1 > > but now the url changes but volgende not. I think number will change; *unless* you happen to retrieve the same number every time, even when you access a different url. What is the result when you run this program, ie, the output of your print statements (then, also, print url)? And, how can url change, but volgende not? Since url depends on volgende. Btw, it may be better to use parentheses in your regular expression to explicitly group whatever you want to match, though the above will work (since it groups the whole match). But Python has this "Explicit is better than implicit" thing. Cheers, Evert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] urllib problem
On Tue, 12 Oct 2010 11:40:17 pm Roelof Wobben wrote: > Hoi, > > I have this programm : > > import urllib > import re > f = > urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php? >nothing=6") inhoud = f.read() > f.close() > nummer = re.search('[0-9]', inhoud) > volgende = int(nummer.group()) > teller = 1 > while teller <= 3 : > url = > "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="; + > str(volgende) f = urllib.urlopen(url) > inhoud = f.read() > f.close() > nummer = re.search('[0-9]', inhoud) > print "nummer is", nummer.group() > volgende = int(nummer.group()) > print volgende > teller = teller + 1 > > but now the url changes but volgende not. > What do I have done wrong ? Each time through the loop, you set volgende to the same result: nummer = re.search('[0-9]', inhoud) volgende = int(nummer.group()) Since inhoud never changes, and the search never changes, the search result never changes, and volgende never changes. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
Sorry about that, I there might have been an obvious reason. *Note that the I invented the *.bob file before you replied. import sqlite3 as lite class db(object): def onNewProjSQLDB(self): self.con = lite.connect('/home/david/pythonfiles/pythonscripts/roughdraftapps/dataplot3/projdir/basicobjb.bob') self.cur = self.con.cursor() self.orderbygname = self.cur.execute('''select * from %s order by graphname''' % ('projectbob')) self.liorder = [] for graphname in self.orderbygname: self.liorder.append(str(graphname[0]).lstrip('u')) print self.liorder # Save (commit) the changes self.con.commit() self.cur.close() self.con.close() db = db() db.onNewProjSQLDB() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
No trickery, I've been adding entries at random all day(scout's honor ii||i). But the above shows the code I used, and the first shows the entries added at random while testing and retrieving, and it shows it alphabetically any other time. I thought it might have to do with a character or several in combination. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
On Tue, Oct 12, 2010 at 9:02 AM, David Hutto wrote: > Sorry about that, I there might have been an obvious reason. > *Note that the I invented the *.bob file before you replied. Apparently, I am to .bob, what Al Gore is to the internet. > > import sqlite3 as lite > class db(object): > def onNewProjSQLDB(self): > self.con = > lite.connect('/home/david/pythonfiles/pythonscripts/roughdraftapps/dataplot3/projdir/basicobjb.bob') > self.cur = self.con.cursor() > self.orderbygname = self.cur.execute('''select * from %s order > by > graphname''' % ('projectbob')) > self.liorder = [] > for graphname in self.orderbygname: > self.liorder.append(str(graphname[0]).lstrip('u')) > print self.liorder > # Save (commit) the changes > self.con.commit() > > self.cur.close() > self.con.close() > db = db() > db.onNewProjSQLDB() > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
"David Hutto" wrote My question is, why is everything except [:-2] in alphabetical order? It doesn't really matter(at this point), for my purposes, but I'd like to know when they changed the abc's to xy;z's? Without seeing the SQL we can't be sure. By default SQL does not guarantee any order. But if you specify an ORDER BY clause then it should be ordered as specified. Did you include an ORDER BY? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
> > Did you include an ORDER BY? See three posts above, line 6. > > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
"David Hutto" wrote > Did you include an ORDER BY? See three posts above, line 6. Looks like our posts crossed in transit :-) So you order by graphname, and you only have a single field of that name? But then when you put it into the list you only use part of graphname converted to a string and stripped: str(graphname[0]).lstrip('u')) So what did the field look like before you modified it? Since that is what determines the way that ORDER BY ordered it? And what is graphname[0] before you convert it to a string? (It looks like it is already a string?!) BTW. Its unusual to have to extract sub fields from a SQL result since to maximise the power of SQL its better to store all "subfields" in separate fields of the table. One of the complexities of debugging any database program is that almost as much depends on the data structures and content as it does on the code. Without seeing the data that is beintg operated upon its hard to be precise. What happens if you execute the SQL at the sqlite interactive prompt? Does it give the same result? PS. You don't need the commit() for a SELECT statement, you aren't changing anything. Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] urllib problem
On Tue, 12 Oct 2010 11:58:03 pm Steven D'Aprano wrote: > On Tue, 12 Oct 2010 11:40:17 pm Roelof Wobben wrote: > > Hoi, > > > > I have this programm : > > > > import urllib > > import re > > f = > > urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.ph > >p? nothing=6") inhoud = f.read() > > f.close() > > nummer = re.search('[0-9]', inhoud) > > volgende = int(nummer.group()) > > teller = 1 > > while teller <= 3 : > > url = > > "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="; + > > str(volgende) f = urllib.urlopen(url) > > inhoud = f.read() > > f.close() > > nummer = re.search('[0-9]', inhoud) > > print "nummer is", nummer.group() > > volgende = int(nummer.group()) > > print volgende > > teller = teller + 1 > > > > but now the url changes but volgende not. > > What do I have done wrong ? > > Each time through the loop, you set volgende to the same result: > > nummer = re.search('[0-9]', inhoud) > volgende = int(nummer.group()) > > Since inhoud never changes, and the search never changes, the search > result never changes, and volgende never changes. Wait, sorry, inhoud should change... I missed the line inhoud = f.read() My mistake, sorry about that. However, I can now see what is going wrong. Your regular expression only looks for a single digit: re.search('[0-9]', inhoud) If you want any number of digits, you need '[0-9]+' instead. Starting from the first URL: >>> f = urllib.urlopen( ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6";) >>> inhoud = f.read() >>> f.close() >>> print inhoud and the next nothing is 87599 but: >>> nummer = re.search('[0-9]', inhoud) >>> nummer.group() '8' See, you only get the first digit. Then looking up the page with nothing=8 gives a first digit starting with 5, and then you get stuck on 5 forever: >>> urllib.urlopen( ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8";).read() 'and the next nothing is 59212' >>> urllib.urlopen( ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=5";).read() 'and the next nothing is 51716' You need to add a + to the regular expression, which means "one or more digits" instead of "a single digit". -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] urllib problem
> From: st...@pearwood.info > To: tutor@python.org > Date: Tue, 12 Oct 2010 23:58:03 +1100 > Subject: Re: [Tutor] urllib problem > > On Tue, 12 Oct 2010 11:40:17 pm Roelof Wobben wrote: >> Hoi, >> >> I have this programm : >> >> import urllib >> import re >> f = >> urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.php? >>nothing=6") inhoud = f.read() >> f.close() >> nummer = re.search('[0-9]', inhoud) >> volgende = int(nummer.group()) >> teller = 1 >> while teller <= 3 : >> url = >> "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="; + >> str(volgende) f = urllib.urlopen(url) >> inhoud = f.read() >> f.close() >> nummer = re.search('[0-9]', inhoud) >> print "nummer is", nummer.group() >> volgende = int(nummer.group()) >> print volgende >> teller = teller + 1 >> >> but now the url changes but volgende not. >> What do I have done wrong ? > > Each time through the loop, you set volgende to the same result: > > nummer = re.search('[0-9]', inhoud) > volgende = int(nummer.group()) > > Since inhoud never changes, and the search never changes, the search > result never changes, and volgende never changes. > > > > -- > Steven D'Aprano > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hello, Here is the output when I print every step in the beginning : inhoud : and the next nothing is 87599 nummer is 8 volgende is 8 and here is the output in the loop : url is: http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8 inhoud is and the next nothing is 59212 nummer is 5 2ste run: url is: http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=5 inhoud is and the next nothing is 51716 nummer is 5 3ste run: url is: http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=5 inhoud is and the next nothing is 51716 nummer is 5 4ste run: I see the problem. It only takes the first number of the nothing. So I have to look how to solve that. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
On Tue, Oct 12, 2010 at 10:23 AM, Alan Gauld wrote: > > "David Hutto" wrote > > > Did you include an ORDER BY? >> >> See three posts above, line 6. >> > > > Just a guess. You strip the letter u from your list items. Is there a letter u in each of them? You are sorting on the item before you strip the u. So, if the one strange item in your list has a u in front, but no others do, it will end up ordering as in your results. >for graphname in self.orderbygname: >self.liorder.append(str(graphname[0]).lstrip('u')) > > __ > -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] urllib problem
> From: st...@pearwood.info > To: tutor@python.org > Date: Wed, 13 Oct 2010 01:51:16 +1100 > Subject: Re: [Tutor] urllib problem > > On Tue, 12 Oct 2010 11:58:03 pm Steven D'Aprano wrote: > > On Tue, 12 Oct 2010 11:40:17 pm Roelof Wobben wrote: > > > Hoi, > > > > > > I have this programm : > > > > > > import urllib > > > import re > > > f = > > > urllib.urlopen("http://www.pythonchallenge.com/pc/def/linkedlist.ph > > >p? nothing=6") inhoud = f.read() > > > f.close() > > > nummer = re.search('[0-9]', inhoud) > > > volgende = int(nummer.group()) > > > teller = 1 > > > while teller <= 3 : > > > url = > > > "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing="; + > > > str(volgende) f = urllib.urlopen(url) > > > inhoud = f.read() > > > f.close() > > > nummer = re.search('[0-9]', inhoud) > > > print "nummer is", nummer.group() > > > volgende = int(nummer.group()) > > > print volgende > > > teller = teller + 1 > > > > > > but now the url changes but volgende not. > > > What do I have done wrong ? > > > > Each time through the loop, you set volgende to the same result: > > > > nummer = re.search('[0-9]', inhoud) > > volgende = int(nummer.group()) > > > > Since inhoud never changes, and the search never changes, the search > > result never changes, and volgende never changes. > > Wait, sorry, inhoud should change... I missed the line inhoud = f.read() > > My mistake, sorry about that. However, I can now see what is going > wrong. Your regular expression only looks for a single digit: > > re.search('[0-9]', inhoud) > > If you want any number of digits, you need '[0-9]+' instead. > > > Starting from the first URL: > > >>> f = urllib.urlopen( > ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=6";) > >>> inhoud = f.read() > >>> f.close() > >>> print inhoud > and the next nothing is 87599 > > > but: > > >>> nummer = re.search('[0-9]', inhoud) > >>> nummer.group() > '8' > > See, you only get the first digit. Then looking up the page with > nothing=8 gives a first digit starting with 5, and then you get stuck > on 5 forever: > > >>> urllib.urlopen( > ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8";).read() > 'and the next nothing is 59212' > >>> urllib.urlopen( > ... "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=5";).read() > 'and the next nothing is 51716' > > > You need to add a + to the regular expression, which means "one or more > digits" instead of "a single digit". > > > > -- > Steven D'Aprano > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Hoi Steven, Finally solved this puzzle. Now the next one of the 33 puzzles. Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] pickle problem
Hello, I have this code : import urllib import pickle image = urllib.URLopener() image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p"; ) plaatje = open("banner.p", "rb") plaatje2 = pickle.load(plaatje) But it gives this output : Traceback (most recent call last): File "C:\Users\wobben\workspace\oefeningen\src\test2.py", line 7, in plaatje2 = pickle.load(plaatje) File "C:\Python27\lib\pickle.py", line 1378, in load return Unpickler(file).load() File "C:\Python27\lib\pickle.py", line 858, in load dispatch[key](self) KeyError: '<' What does this mean ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...)
Thanks Chris and Alan, OK, I see. Now that I managed to build the dictionary, I did a print to confirm that indeed the dictionary was created and it had the intended contents and I was surprised to see that the order of the items in it was totally changed. So the text file from which the dictionary was created was sorted in alphabetical order ('A-01,1334', 'A-02,1234',...'I-01,1334'...), but when I print the dictionary, I get: {'I-02': '1399', 'I-01': '1374', 'D-09': '1524', 'I-07': '1399' .} I don't think this will be a problem for what I want to do next but I'm curious to know why the order is all changed in a way that doesn't seem to be very intuitive. Josep M. On Mon, Oct 11, 2010 at 2:23 PM, Christian Witts wrote: > > What you should be doing is: > > fileNameCentury = > open('/Volumes/DATA/Documents/workspace/GCA/CORPUS_TEXT_LATIN_1/FileNamesYears.txt', > 'r') > dct = {} > for line in fileNameCentury: #File objects have built-in iteration >key, value = line.strip().split(',') >dct[key] = value > > Hope that helps. > It did. A great deal! Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...)
On Tue, Oct 12, 2010 at 1:52 PM, Josep M. Fontana wrote: > Thanks Chris and Alan, > > OK, I see. Now that I managed to build the dictionary, I did a print to > confirm that indeed the dictionary was created and it had the intended > contents and I was surprised to see that the order of the items in it was > totally changed. So the text file from which the dictionary was created was > sorted in alphabetical order ('A-01,1334', 'A-02,1234',...'I-01,1334'...), > but when I print the dictionary, I get: {'I-02': '1399', 'I-01': '1374', > 'D-09': '1524', 'I-07': '1399' .} > > I don't think this will be a problem for what I want to do next but I'm > curious to know why the order is all changed in a way that doesn't seem to > be very intuitive. > Here is a discussion of how to iterate over a dictionary in sorted order: http://stackoverflow.com/questions/364519/in-python-how-to-i-iterate-over-a-dictionary-in-sorted-order -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...)
On Tue, Oct 12, 2010 at 2:46 PM, Josep M. Fontana wrote: > > > On Tue, Oct 12, 2010 at 8:37 PM, Joel Goldstick > wrote: > >> >> >> On Tue, Oct 12, 2010 at 1:52 PM, Josep M. Fontana < >> josep.m.font...@gmail.com> wrote: >> >>> Thanks Chris and Alan, >>> >>> OK, I see. Now that I managed to build the dictionary, I did a print to >>> confirm that indeed the dictionary was created and it had the intended >>> contents and I was surprised to see that the order of the items in it was >>> totally changed. So the text file from which the dictionary was created was >>> sorted in alphabetical order ('A-01,1334', 'A-02,1234',...'I-01,1334'...), >>> but when I print the dictionary, I get: {'I-02': '1399', 'I-01': '1374', >>> 'D-09': '1524', 'I-07': '1399' .} >>> >>> I don't think this will be a problem for what I want to do next but I'm >>> curious to know why the order is all changed in a way that doesn't seem to >>> be very intuitive. >>> >> >> Here is a discussion of how to iterate over a dictionary in sorted order: >> >> >> http://stackoverflow.com/questions/364519/in-python-how-to-i-iterate-over-a-dictionary-in-sorted-order >> > > > Thanks very much, Joel, for your quick response. This information might be > useful but my question was really more out of curiosity to know how Python > works. I don't understand why a dictionary that was created by iterating > over a list that was sorted turns out to be unsorted. I would not have been > surprised if the order was reversed, but the order that I got makes no sense > to me. What is the logic of the order in which the items in this dictionary > are arranged? > > JM > > >> >> Well, dictionaries use keys which are stored in hash tables. A dictionary is a mapping. It doesn't require an order. It just requires that given a key, it will return a value. While I don't know enough about this to be instructive, it makes looking up values faster. When the dictionary is retrieved, its order depends on the hashed values rather than the keys themself. If you play around with a dictionary, adding new key-value pairs, then displaying the dict, you might find that the order changes in no particular way. Here is a wikipedia article. http://en.wikipedia.org/wiki/Hash_table A list, on the other hand IS ordered (its a squence), and so when you add or remove values from a list, the order is maintained. -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...)
On 12 October 2010 21:15, Joel Goldstick wrote: > When the dictionary is retrieved, its order depends on the hashed values > rather than the keys themself. If (big IF here) you really need an ordered dict you can use the OrderedDict from the collections module. However this will only guarantee *insertion* order (except for existing keys). Do make sure you read pep 372 [1]. Greets Sander [1] http://www.python.org/dev/peps/pep-0372/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using contents of a document to change file names, (was Re: how to extract data only after a certain ...)
On Tue, Oct 12, 2010 at 3:39 PM, Sander Sweers wrote: > On 12 October 2010 21:15, Joel Goldstick wrote: > > When the dictionary is retrieved, its order depends on the hashed values > > rather than the keys themself. > > If (big IF here) you really need an ordered dict you can use the > OrderedDict from the collections module. However this will only > guarantee *insertion* order (except for existing keys). Do make sure > you read pep 372 [1]. > > Greets > Sander > > [1] http://www.python.org/dev/peps/pep-0372/ > ___ > It seems a common practice to create an ordered list of the keys, then iterate over the list to operate on the dictionary. -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Compiling python and SQlite3 prob on ubuntu 10.10
Hi, I am using the recently released python10.10 and wanted to install python2.5.4 on my system to do some app engine development, and this is what I did:- 1.I downloaded Python-2.5.4.tgz.tar from the python site. 2.cd Downloads tar -xvzf Python-2.5.4.tgz.tar cd Python-2.5.4 ./configure --prefix=/usr/local/python2.5.4 make make test sudo make install sudo ln -s /usr/local/python2.5.4/bin/python /usr/bin/python2.5.4 However after installing it, when I try using it , I get the below error:- File "/home/vivek/google_appengine/google/appengine/datastore/datastore_sqlite_stub.py", line 52, in import sqlite3 File "/usr/local/python2.5.4/lib/python2.5/sqlite3/__init__.py", line 24, in from dbapi2 import * File "/usr/local/python2.5.4/lib/python2.5/sqlite3/dbapi2.py", line 27, in from _sqlite3 import * ImportError: No module named _sqlite3 How do I get to solve this. Need someone to help me here please! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pickle problem
Am 12.10.2010 19:35, schrieb Roelof Wobben: Hello, I have this code : import urllib import pickle image = urllib.URLopener() image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p"; ) plaatje = open("banner.p", "rb") plaatje2 = pickle.load(plaatje) But it gives this output : Traceback (most recent call last): File "C:\Users\wobben\workspace\oefeningen\src\test2.py", line 7, in plaatje2 = pickle.load(plaatje) File "C:\Python27\lib\pickle.py", line 1378, in load return Unpickler(file).load() File "C:\Python27\lib\pickle.py", line 858, in load dispatch[key](self) KeyError: '<' The last error indicates that a python module (pickle.py) tried to access a dictionary (dispatch) with the key "<". The expected value seems to be a callable as indicated by the (self). But there's no key "<" in it. So, probably, this module was called with improper data. If you follow up your stack trace to where the error in your code occurs, you'll see that something is wrong with "unpickling" the data in your plaatje file. Obviously, the data could be passed to the pickle module. So no read error on the file. What's left? There's probably an issue with the data in the file. Now, you have to know that pickling reads and writes Python objects in a certain format. That means you can only load data with pickling, which was created by pickling. Question: Is your data in "banner.p" properly formatted pickling data? Note: There're different formats for pickling. Check the docs or a very nice source for examples of the Python Standard lib: PyMOTW (Google first hit); there's an article about pickling among others. But first should be the standard documentation. What does this mean ? Roelof ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pickle problem
On 12/10/10 18:35, Roelof Wobben wrote: Hello, I have this code : import urllib import pickle image = urllib.URLopener() image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p"; ) plaatje = open("banner.p", "rb") plaatje2 = pickle.load(plaatje) But it gives this output : Traceback (most recent call last): File "C:\Users\wobben\workspace\oefeningen\src\test2.py", line 7, in plaatje2 = pickle.load(plaatje) File "C:\Python27\lib\pickle.py", line 1378, in load return Unpickler(file).load() File "C:\Python27\lib\pickle.py", line 858, in load dispatch[key](self) KeyError: '<' What does this mean ? It means that it's trying to access a sequence with the key '<' but it's not working. It looks like the problem is you're trying to open peak.html as a pickle but it is actually an html file. HTH, Adam. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pickle problem
On Tue, Oct 12, 2010 at 1:35 PM, Roelof Wobben wrote: > > > Hello, > > I have this code : > > import urllib > import pickle > > image = urllib.URLopener() > image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p"; > ) > plaatje = open("banner.p", "rb") > plaatje2 = pickle.load(plaatje) > > But it gives this output : > > Traceback (most recent call last): > File "C:\Users\wobben\workspace\oefeningen\src\test2.py", line 7, in > >plaatje2 = pickle.load(plaatje) > File "C:\Python27\lib\pickle.py", line 1378, in load >return Unpickler(file).load() > File "C:\Python27\lib\pickle.py", line 858, in load >dispatch[key](self) > KeyError: '<' > > > What does this mean ? > I have heard of pickle, but never used it. I looked it up in Learning Python (O'Reilley p 236). The example they give shows that you need to pickle your object, and put it in a file with pickle(dump(D,F) where D is a dictionary in the example and F is a file object. You then can retrieve it with pickle.load(F). The thing you are trying to load is a file that was a copy of the contents of the url. It is html, and not 'pickled' I think you are missing a line or two of code from your example tutorial > > Roelof > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pickle problem
On 12 October 2010 18:35, Roelof Wobben wrote: > image = urllib.URLopener() > image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p"; > ) > OK firstly, image is an URLopener, so whatever URL you specify, that's the file that it will download. (I would therefore suggest you give it a slightly better name next time -- you're after all not even going to be downloading an image so the name "image" is arguably just confusing.) Now, back to the problem. Look closely, what file are you downloading? Right, you're downloading "http://www.pythonchallenge.com/pc/def/peak.html"; However... that's the HTML page itself! So, what you're doing is to download the "peak.html" HTML file from the site, and save it locally as "banner.p"... But... what you really want is the "banner.p" file *on the website*. (As alluded to in the page source, if you view the web page above in your browser...) So..., you need to construct the URL to the "banner.p" pickle file on the website and then retrieve it. (Hint: Just replace peak.html with banner.p in the page URL) You don't even have to do this with Python, you can fetch the pickle file by hand with your browser first if you want, just punch the URL into your browser and watch it download the banner.p file. (But, it's fun to do it in Python! ) Apologies if I've given away too much! Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] urllib problem
"Roelof Wobben" wrote Finally solved this puzzle. Now the next one of the 33 puzzles. Don;t be surprised if you get stuck. Python Challenge is quite tricky and is deliberately designed to make you explore parts of the standard library you might not otherwise find. Expect to do a lot of reading in the documebntation. Its really targeted at intermediate rather than novice programmers IMHO. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] pickle problem
"Roelof Wobben" wrote image = urllib.URLopener() image.retrieve("http://www.pythonchallenge.com/pc/def/peak.html","banner.p"; ) Roelof, Please do not post a series of request for help on each of the Python Challenge puzzles. They are intended to be challenging and to force you to consult the documentation so that you learn how to use the modules. But asking for help on this list will simply give the puzzles away to anyone else on the list who may be trying them or intending to try them in the future. plaatje = open("banner.p", "rb") plaatje2 = pickle.load(plaatje) You are using the pickle module so have you read the documentation on pickle? Have you read examples in other tutorials on how to use pickle? Have you experimented with pickle and got it to work outside the context of the challenge? What does this mean ? It means you are not using pickle properly. Please read the documentation. Sorry to sound harsh but this is different to you trying to understand a tutorial and its exercises. This is supposed to be a challenge for you personally not the tutor list en-masse. If you get really stuck then ask for hints, but please do your homework first. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
On Tue, Oct 12, 2010 at 10:23 AM, Alan Gauld wrote: > > "David Hutto" wrote > >> > Did you include an ORDER BY? >> >> See three posts above, line 6. > > Looks like our posts crossed in transit :-) > > So you order by graphname, and you only have a single field of that name? > But then when you put it into the list you only use part of graphname > converted > to a string and stripped: This is actually an example derived from the code I used it in. The full code is here: http://pastebin.com/9H54fEYb I forgot to give the names in the link, but the .py files are seperated, and should be named as follows. The first module should be named dataplotmain.py The second should be named dpappfunc.py The third should be named mainpanel.py and the dbfile should be in the directory placed on line 58 of mainpanel.py It's still in rough draft form, but feel free to comment, and not some will be taken out, and much more placed in. > > str(graphname[0]).lstrip('u')) > > So what did the field look like before you modified it? Since that is what > determines the way that ORDER BY ordered it? And what is > graphname[0] before you convert it to a string? (It looks like it > is already a string?!) Below is the output before converting: self.liorder.append(graphname) to self.liorder.append(str(graphname[0]).lstrip('u')) [(u'.hjvkjgfkj/bdgfkjbg', u''), (u'uuz', u'Pie Chart'), (u'jgkgyckghc', u''), (u'kfhhv ', u''), (u'khfhf', u''), (u'test', u''), (u'test10', u''), (u'test2', u'Dashed/Dotted'), (u'test3', u'Pie Chart'), (u'test346w43', u''), (u'test4', u'Scatter Plot'), (u'test5', u''), (u'test6', u''), (u'test7', u''), (u'test7uyuy', u''), (u'test8', u''), (u'test9', u''), (u'testgraph', u''), (u'u;s;juf;sfkh', u''), (u'zzrerhshhjrs', u'')] > > BTW. > Its unusual to have to extract sub fields from a SQL result since to > maximise the power of SQL its better to store all "subfields" in > separate fields of the table. Almost to that part. As you can see from the full script in the link above, it's still early on, so I just got the insert into db portion in, and was working on accessing it, before placing in the overall data structure I want to use. > > One of the complexities of debugging any database program is that > almost as much depends on the data structures and content as it > does on the code. Without seeing the data that is beintg operated > upon its hard to be precise. The above code should clarify more than my original posting. > > What happens if you execute the SQL at the sqlite interactive > prompt? Does it give the same result? haven't used the sqlite prompt yet, Haven't needed to yet. > > > PS. You don't need the commit() for a SELECT statement, you aren't > changing anything. Consider it removed. > > > Alan G. > > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] SQLite3 DB Field Alphabetizing
Quick note: forgot to add that the initial db file is setup with the following: import sqlite3 as lite class db(object): def onNewProjSQLDB(self): self.con = lite.connect('/home/david/pythonfiles/pythonscripts/roughdraftapps/dataplot3/db/dpdb.db') self.cur = self.con.cursor() #rows and columns are singular in project, and position is individual to graph/overlayed graphs self.cur.execute('''create table projectbob(graphname, typeograph )''') # Save (commit) the changes self.con.commit() self.cur.close() self.con.close() db = db() db.onNewProjSQLDB() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor