Comparing strings - akin to Perl's "=~"

A.T.Hofkamp hat at se-162.se.wtb.tue.nl
Tue May 6 09:23:35 EDT 2008


On 2008-05-06, krumblebunk at gmail.com <krumblebunk at gmail.com> wrote:
> Hello gurus,

Hello fellow-guru,

> 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

I am sure the above is very good. However, as I never program a data base, I
absolutely have no clue what data structure you assume here. A literal value in
Python syntax or so would be quite useful.

>     f=dict(foo)

f becomes a dict here. Not sure why you do this, as you stated above that you
already had a dict.

>     for k in f.iteritems()

k becomes here all key/value pairs of f, ie it is a tuple
example:

f = {1:'a', 2:'b'}
for k in f.iteritems():
  print k

will print something like

(1, 'a')
(2, 'b')

A more useful statement could be

for k,v in f.iteritems():
   ...

here a multi-assignment is done (the tuple is broken and each part is assigned
to a seperate variable), k becomes the key-part, and v becomes the value-part

>         if k == '^Hostname':               <-- need this sort of
> behaviour - match a partial string.

This will break in two ways. Firstly, k was a tuple, so you cannot compare it
with a string. Secondly, this is not the way to match partial strings.

The first problem can be solved with the multi-assignment, or by indexing on
the tuple (ie k[0] or k[1]).

For the second problem, have a look at the string methods:
http://docs.python.org/lib/string-methods.html#l2h-233

In this particular case, you can use
if k.startswith('Hostname'):
or
if k[:8] == 'Hostname': # Slice the first 8 chars of the string

For more powerful regular expressions, read the 're' module documentation.


>         print "%s" % f[3]              <-- ..and if true, need to pull
> out the 4th column on that line.

f is a dict. That means indexing is on its keys. Unless you have the integer
key 3 in your dictionary, this will not work.

f = {3:'q'}
print f[3]  # prints 'q'

However, a dict has no order, so the '4th column' has no meaning.

On the other hand, if f is a list, like [10,20,30,40,50], then f[3] is 50,
because you can use integers to index in a list (indexing is just a simple form
of slicing).

> This breaks spectacularly -

Ah, segmentation fault?
(that would be spectacular at least).

It would also help if you tell us at what line it breaks and with what error.


> any ideas?

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]





More information about the Python-list mailing list