Why is the result of a recordset returned in this way to query a Python database?

I searched high and low to answer the question why the query results are returned in this format and how to convert to a list.

data = cursor.fetchall ()

When I print the data, it results in: ("car",), ("boat",), ("plane",), ("truck"))

I want to get results in a list like ["car", "boat", "plane", "truck"]

+3
source share
3 answers

He returns it this way, because the record set consists of many rows of data, not a list of individual elements.

, :

data = [row[0] for row in cursor.fetchall()]
+5

fetchall() , .

, , .

+1
x = (('car',), ('boat',), ('plane',), ('truck',))

y = [z[0] for z in x] # ['car', 'boat', 'plane', 'truck']
0
source

Source: https://habr.com/ru/post/1743030/


All Articles