I am new to Python, and I am trying to use Python to query a Microsoft SQL Server database. The data is returned in this format:
(('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
What I am trying to do is check to find a match, to see if some data from another data table exists in that data from SQL Server.
When I try to use the "in" to check, it does not work for me. I thought if I could convert the data (a tuple of tuples) into a list, then I could more easily search and match. But that doesn't work because there are some brackets around each list element. At least, that is the what I think because, if I manually recreate the list without the extra brackets, then I can search successfully.
I am wondering if there is a way to remove that extra bracket. Or, maybe there is a better approach. I have read several posts here and other articles, and so far, I have not found an approach.
Here is what I have tried. As you can see, the final one works, but that is what i have created manually.
# here is the data that gets returned from Msft Sql Server
sql_data = (('orange,apple,coconut',), ('lettuce,carrot,celery',), ('orange,lemon,strawberry',))
print(sql_data)
print('type of rds_qubole_data is {}'.format(type(sql_data)))
# the type shows as <class 'tuple'>
# set some variables for checking in the data
Orange1 = 'orange,apple,coconut' in sql_data
print('orange1 is {}'.format(Orange1))
# shows orange1 is False
Vegan1 = 'lettuce,carrot,celery' in sql_data
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is False
# then, i try to convert to a list using a method i saw in another post
conv_to_list = [list(tup) for tup in sql_data]
print(conv_to_list)
# data looks like:
# ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print('type of conv_to_list is {} '.format(type(conv_to_list)))
# prints type of conv_to_list is <class 'list'>
Orange1 = 'orange,apple,coconut' in conv_to_list
print('orange1 is {}'.format(Orange1))
# orange1 is False
Vegan1 = 'lettuce,carrot,celery' in conv_to_list
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is False
# finally, i just manually type the list and remove the brackets around each element
manual_list = ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print(manual_list)
# ['orange,apple,coconut', 'lettuce,carrot,celery', 'orange,lemon,strawberry']
print('Type of manual_list is {}'.format(type(manual_list)))
# Type of manual_list is <class 'list'>
Orange1 = 'orange,apple,coconut' in manual_list
print('orange1 is {}'.format(Orange1))
# orange1 is True
Vegan1 = 'lettuce,carrot,celery' in manual_list
print('Vegan1 is {}'.format(Vegan1))
# Vegan1 is True