[Tutor] Checking for string in a list strangeness

Kent Johnson kent37 at tds.net
Mon Apr 6 14:54:17 CEST 2009


On Mon, Apr 6, 2009 at 8:26 AM, AdamC <kabads at gmail.com> wrote:
> 2009/4/6 Kent Johnson <kent37 at tds.net>:
>> On Mon, Apr 6, 2009 at 3:30 AM, AdamC <kabads at gmail.com> wrote:
>>> I'm writing a small cgi application for children to use and I want to
>>> check that the name they enter isn't a swear word.
>>
>>> #for i in swearlist:       # shows swear list OK
>>> #    print i;
>>
>> Perhaps the words in swearlist include some  whitespace? Try
>> for i in swearlist:
>>  print repr(i)
>>
>> and look for leading and trailing spaces, tabs, etc.
>>
>> Kent
>
> I think you're on to something here Kent. Thanks. There doesn't appear
> to be any whitespaces in the words but an entry in the list appears
> like this:
>
> print swearlist[0]
> returns
> ('xxxx',)
>
> where xxxx is the expletive in the database.

Ah, yes, the result of mycursor.fetchone() is a tuple containing the
fetched elements. Even though you are only reading one field, it is
still returned in a tuple. That is what the parentheses and comma
signify. Try this:

swearlist = []
for i in range (0, myrowcount):
   myrow = mycursor.fetchone()
   swearlist.append(myrow[0])

Kent


More information about the Tutor mailing list