By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 1,051 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

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

P: n/a
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='pp p')
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.

Jun 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
kr*********@gmail.com wrote:
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='pp p')
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?
Use the module "re" to create regular expressions & out of these
mather-objects that will give you what you need.

The idiom is roughly translated to this:

m = re.search(r"^Hostname')
if m:
...

Diez
Jun 27 '08 #2

P: n/a
kr*********@gmail.com wrote:
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 -
Several observations:

* "breaks spectacularly" is not a useful description of a failure. If
you get an exception, show us the exception. If you get an unexpected
result, show us the result and tell us how it differs from the result
you expected.

* To check whether a string starts with a given string, use the
startswith() method: if k.startswith("Hostname"): ...

* Why the "f=dict(foo)"? Isn't foo already a dictionary?

* Since f is a dictionary, presumably keyed on column names, f[3] is
unlikely to work. You're going to have to use the name of the fourth
column to pull out the value you're looking for.

HTH,

--
Carsten Haese
http://informixdb.sourceforge.net
Jun 27 '08 #3

P: n/a
On May 6, 2:23 pm, "A.T.Hofkamp" <h...@se-162.se.wtb.tue.nlwrote:

.....
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.
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.