Comparing strings - akin to Perl's "=~"
Hello gurus, I am learning Python to take the place of Perl in my toolbox of bits and bobs, and writing something pretty simple in theory, but having a hard time in Python with it - I am using a 3rd party module, and I am sure the etiquette of this channel states that this is pure Python questions only, but please bare with me in light of this, as my question does indeed relate to Python only. I am grabbing the output from a SQL statement (using PyGreSQL 'pg' module), and when it returns the result, I am pulling it out as such: try: sc=pg.connect(dbname='mydb',host='dbhost',user='ppp') except pg.InternalError: print "Failed to execute SQL: %s" % sql exit for foo in sc.query(sql).dictresult(): <- this returns a dict of the result f=dict(foo) for k in f.iteritems() if k == '^Hostname': <-- need this sort of behaviour - match a partial string. print "%s" % f[3] <-- ..and if true, need to pull out the 4th column on that line. This breaks spectacularly - any ideas? thanks! kb. -- http://mail.python.org/mailman/listinfo/python-list
Re: Comparing strings - akin to Perl's "=~"
On May 6, 2:23 pm, "A.T.Hofkamp" <[EMAIL PROTECTED]> wrote:
> Without a explicit Python value of what comes out of the sql query, I can only
> guess. As example, assume the following data:
>
> f = { 1: ['Hostname', 'blabla', 'person', 'john'],
> 2: ['MachineName', 'blabla', 'company', 'something']}
>
> ie a dictionary of column number to rows.
>
> Here you are only interested in the value part, and the code becomes.
>
> for v in f.itervalues():
> if v[0].startswith('Hostname'):
> print v[3]
Many thanks for all your replies - this last snippet of code helped
me, extra thanks!
kb.
--
http://mail.python.org/mailman/listinfo/python-list
Reversing a dict?
Hi - further to my earlier query regarding partial matches (which with all your replies enabled me to advance my understanding, thanks), I now need to reverse a dict. I know how to reverse a list (with the reverse method - very handy), but it doesn't seem possible to reverse a dict. I suspect what I need to do is somehow go from: thelist=list(thedict) thelist.reverse() thedict=dict(thelist) Does anyone know how to convert / or reverse a dict? thanks kb. -- http://mail.python.org/mailman/listinfo/python-list
Re: Reversing a dict?
Thanks all!! kb. -- http://mail.python.org/mailman/listinfo/python-list
