Matching Strings
Gabriel Genellina
gagsl-py at yahoo.com.ar
Fri Feb 9 20:00:06 EST 2007
En Fri, 09 Feb 2007 21:03:32 -0300, <rshepard at nospam.appl-ecosys.com>
escribió:
> I'm not sure how to change a string so that it matches another one.
>
> My application (using wxPython and SQLite3 via pysqlite2) needs to
> compare
> a string selected from the database into a list of tuples with another
> string selected in a display widget.
>
> An extract of the relevant code is:
>
> selName = self.polTree.GetItemText(selID)
> ...
> for item in self.appData.polNat:
> print 'Item: ', item, '\n', 'selName: ', selName, '\n'
> if item == selName:
> print '***** ', self.appData.polNat[1]
>
> The last comparison and print statement never work because the strings
> are
> presented this way:
>
> Item: (u'ground water',)
> selName: ground water
Forget about re and slicing and blind guessing...
item appears to be a tuple; in these cases repr is your friend. See what
happens with:
print repr(item), type(item)
If it is in fact a tuple, you should ask *why* is it a tuple (maybe could
have many items?). And if it's just an artifact and actually it always
will be a single:
assert len(item)==1
item = item[0]
if item...
--
Gabriel Genellina
More information about the Python-list
mailing list