473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Matching Strings

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
Feb 10 '07 #1
7 1715
rs******@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.

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
Feb 10 '07 #2
On Feb 9, 6:03 pm, rshep...@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.

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:
>>u'a' == 'a'
True

-- Paul

Feb 10 '07 #3
En Fri, 09 Feb 2007 21:03:32 -0300, <rs******@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

Feb 10 '07 #4
On 2007-02-10, James Stroud <js*****@mbi.ucla.eduwrote:
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
Feb 10 '07 #5
On Feb 10, 11:03 am, rshep...@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

Feb 10 '07 #6
On Feb 10, 12:01 pm, rshepard-at-appl-ecosys.com wrote:
On 2007-02-10, James Stroud <jstr...@mbi.ucla.eduwrote:
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
AArrgghh it's not a list index, it's a ferschlugginer tuple containing
1 element.

Feb 10 '07 #7
On Fri, 09 Feb 2007 16:17:31 -0800, James Stroud wrote:
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]
>>timeit.Timer( \
.... '''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.

--
Steven.

Feb 10 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: | last post by:
Hi, I'm fairly new to regular expressions, and this may be a rather dumb question, but so far I haven't found the answer in any tutorial or reference yet... If I have f.i. the string "The...
3
by: Day Of The Eagle | last post by:
Jeff_Relf wrote: > ...yet you don't even know what RegEx is. > I'm looking at the source code for mono's Regex implementation right now. You can download that source here ( use the class...
0
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful...
10
by: javuchi | last post by:
I'm searching for a library which makes aproximative string matching, for example, searching in a dictionary the word "motorcycle", but returns similar strings like "motorcicle". Is there such a...
5
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I...
4
by: Lauren Wilson | last post by:
Hi folks, We have a need to replace sub strings in certain message text. We use the Office Assistant to display help and often use the imbedded formatting commands. Those of you who have used...
7
by: Kevin CH | last post by:
Hi, I'm currently running into a confusion on regex and hopefully you guys can clear it up for me. Suppose I have a regular expression (0|(1(01*0)*1))* and two test strings: 110_1011101_ and...
2
by: Ole Nielsby | last post by:
First, bear with my xpost. This goes to comp.lang.c++ comp.lang.functional with follow-up to comp.lang.c++ - I want to discuss an aspect of using C++ to implement a functional language, and...
9
by: Chris | last post by:
Is anyone aware of any prior work done with searching or matching a pattern over nested Python lists? I have this problem where I have a list like: , 9, 9], 10] and I'd like to search for the...
11
by: tech | last post by:
Hi, I need a function to specify a match pattern including using wildcard characters as below to find chars in a std::string. The match pattern can contain the wildcard characters "*" and "?",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.