Reputation: 3
I've got
data = list(a.fetchall())
print data
It returns:
[('Data1',), ('Data2',), ('Data3',), ('Data4',), ('Data5',), ('Data6',), ('Data7 ',), ('Data8',)]
How could I get those values separated?
I tried to use
print ', '.join(data)
But couldn't get a good return of values.
The return should be like
data1,data2,data3...
And can save the data in different variables if possible...
Upvotes: 0
Views: 70
Reputation: 55469
Here's a functional solution.
from operator import itemgetter
data = [
('Data1',), ('Data2',), ('Data3',), ('Data4',),
('Data5',), ('Data6',), ('Data7 ',), ('Data8',)
]
# Extract the string from each tuple into a new list
newdata = map(itemgetter(0), data)
# Join the strings in the list into a single string
print newdata
print ', '.join(newdata)
output
['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6', 'Data7 ', 'Data8']
Data1, Data2, Data3, Data4, Data5, Data6, Data7 , Data8
Upvotes: 1
Reputation: 599520
You have a list of one-item tuples. You need to extract the values from the tuples themselves.
', '.join(element[0] for element in data)
Upvotes: 1