Matching Strings

R

rshepard

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

What do I need to do to 'Item' to strip the parentheses, unicode symbol,
single quotes, and comma? Do I want 'raw' output? If so, how do I specify
that in the line 'if item == selName:'?

TIA,

Rich
 
J

James Stroud

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

What do I need to do to 'Item' to strip the parentheses, unicode symbol,
single quotes, and comma? Do I want 'raw' output? If so, how do I specify
that in the line 'if item == selName:'?

TIA,

Rich

Assuming item is "(u'ground water',)"

import re
item = re.compile(r"\(u'([^']*)',\)").search(item).group(1)

James
 
P

Paul McGuire

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

What do I need to do to 'Item' to strip the parentheses, unicode symbol,
single quotes, and comma? Do I want 'raw' output? If so, how do I specify
that in the line 'if item == selName:'?

TIA,

Rich

I suspect that the variable item is *not* a string, but a tuple whose
zero'th element is a unicode string with the value u'ground water'.
Try comparing item[0] with selname.
From my command prompt:
True

-- Paul
 
G

Gabriel Genellina

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...
 
R

rshepard-at-appl-ecosys.com

Assuming item is "(u'ground water',)"

import re
item = re.compile(r"\(u'([^']*)',\)").search(item).group(1)

James,

I solved the problem when some experimentation reminded me that 'item' is
a list index and not a string variable. by changing the line to,

if item[0] == selName:

I get the matchs correctly.

Now I need to extract the proper matching strings from the list of tuples,
and I'm working on that.

Many thanks,

Rich
 
J

John Machin

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
 
S

Steven D'Aprano

Assuming item is "(u'ground water',)"

import re
item = re.compile(r"\(u'([^']*)',\)").search(item).group(1)

Using a regex is a lot of overhead for a very simple operation.

If item is the string "(u'ground water',)"

then item[3:-3] will give "ground water".
import re, timeit
item = "(u'ground water',)"
timeit.Timer('item[3:-3]', 'from __main__ import item').repeat()
[0.56174778938293457, 0.53341794013977051, 0.53485989570617676]
.... '''re.compile(r"\(u'([^']*)',\)").search(item).group(1)''', \
.... 'from __main__ import item; import re').repeat()
[9.2723720073699951, 9.2299859523773193, 9.2523660659790039]


However, as many others have pointed out, the Original Poster's problem
isn't that item has leading brackets around the substring he wants, but
that it is a tuple.
 
D

Dennis Lee Bieber

I'm not sure how to change a string so that it matches another one.
By now you've been inundated with the answer to the immediate
problem -- namely that result sets obtained from a database query, even
if only one column was selected, come back as a list of tuples*, and you
need to index the field from the tuple.

However, I'm seeing some sort of design disconnect in what was
posted here. As I understand it, you are: 1) getting a text string from
a user; 2) retrieving a single column from some database table; 3)
looping over this result set to see if the user string is in one of the
records returned from the database...

Wouldn't it be simpler to: 1) get a text string from the user; 2)
submit a query to the database /using that text string/ returning the
count of occurrences.

select count(*) from atable where acolumn = "user string"

result set should be (0,) if not in table, (>0,) if it exists.

OR maybe better... DON'T use a text string input. 1) query the
database for all values in the selected column; 2) populate a list (or
combo) box from these values; 3) let the user select from this list.
BINGO -- a known value (or for combo boxes, a known value or a flag that
the user typed in something that isn't in the list, assuming you might
want to save such into the database).




* Except for those that support returning a list of dictionaries, in
which case you must key off the column name.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top