Constantine
Constantine

Reputation: 203

Python LOB to List

Using:

cur.execute(SQL)
response= cur.fetchall() //response is a LOB object
names = response[0][0].read()

i have following SQL response as String names:

'Mike':'Mike'
'John':'John'
'Mike/B':'Mike/B' 

As you can see it comes formatted. It is actualy formatted like:\\'Mike\\':\\'Mike\\'\n\\'John\\'... and so on

in order to check if for example Mike is inside list at least one time (i don't care how many times but at least one time)

I would like to have something like that:

l = ['Mike', 'Mike', 'John', 'John', 'Mike/B', 'Mike/B'],

so i could simply iterate over the list and ask

for name in l: 
   'Mike' == name: 
      do something

Any Ideas how i could do that?

Many thanks

Edit:

When i do:

list = names.split()

I receive the list which is nearly how i want it, but the elements inside look still like this!!!:

list = ['\\'Mike\\':\\'Mike\\", ...]

Upvotes: 0

Views: 184

Answers (2)

joel goldstick
joel goldstick

Reputation: 4493

names = ['\\'Mike\\':\\'Mike\\", ...]

for name in names:
    if "Mike" in name:
        print "Mike is here"

The \\' business is caused by mysql escaping the '

if you have a list of names try this:

my_names = ["Tom", "Dick", "Harry"]
names = ['\\'Mike\\':\\'Mike\\", ...]

for name in names:
    for my_name in my_names:
        if myname in name:
        print myname, " is here"

Upvotes: 1

Moses Koledoye
Moses Koledoye

Reputation: 78554

import re
pattern = re.compile(r"[\n\\:']+")
list_of_names = pattern.split(names) 
# ['', 'Mike', 'Mike', 'John', 'John', 'Mike/B', '']
# Quick-tip: Try not to name a list with "list" as "list" is a built-in

You can keep your results this way or do a final cleanup to remove empty strings

clean_list = list(filter(lambda x: x!='', list_of_names))

Upvotes: 1

Related Questions