Matching Strings
John Machin
sjmachin at lexicon.net
Fri Feb 9 20:04:43 EST 2007
On Feb 10, 11:03 am, rshep... at nospam.appl-ecosys.com wrote:
> 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.
Tuple? Doesn't that give you a clue?
>
> 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:
What you mean is: The way you have presented the strings is confusing
you, and consequently you have written a comparison that will not
work :-)
>
> Item: (u'ground water',)
Hmmmm. That comma in there is interesting. I wonder where the
parentheses came from. What did the man mutter about a list of tuples?
> selName: ground water
>
> What do I need to do to 'Item' to strip the parentheses, unicode symbol,
> single quotes, and comma?
Nothing. They're not there. It's all in your mind.
> Do I want 'raw' output?
What is "raw output"?
> If so, how do I specify
> that in the line 'if item == selName:'?
That's a comparison, not output.
Step 1: Find out what you've *really* got there:
Instead of
print 'Item: ', item, '\n', 'selName: ', selName, '\n'
do this:
print 'item', repr(item), type(item)
print 'selName', repr(selName), type(selName)
Step 2:
Act accordingly.
Uncle John's Crystal Balls (TM) predict that you probably need this:
if item[0] == selName:
HTH,
John
More information about the Python-list
mailing list