0

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\\", ...]
Anil_M
  • 10,893
  • 6
  • 47
  • 74
Constantine
  • 193
  • 1
  • 3
  • 11
  • This is not an answer to your question, but you can test for the presence of an element inside a list in a more "pythonic" way, like this: `if 'Mike' in l: ...` – user2340612 Mar 31 '16 at 12:03
  • The main problem is: I don't know how to make out of this formatted String a list... – Constantine Mar 31 '16 at 12:05

2 Answers2

1
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))
Moses Koledoye
  • 77,341
  • 8
  • 133
  • 139
1
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"
joel goldstick
  • 4,393
  • 6
  • 30
  • 46
  • "The \\' business is caused by mysql escaping the '" This is what i didn't knew. With this information i can simply split " ' " out. Thanks – Constantine Mar 31 '16 at 13:26