Record datatype equivalent in python

I am connecting to snowfalke database and fetch only 1 record. This record will have 21 columns. After i read them in python using connection, they return a tuple object.

I want to access the elements of tuple object with a name.

Preferrably i want to access these tuple elemts with the same name as column name from database.

In PL/SQL, we have something called as record type. And the elements in record can be accessed with a name

How do i do this in python?

Pseudocode ex:

tup_obj = connection.execute('select name, age, height, weight from table')

I want to access the tuple object like with names like this,

tup_obj.name == 'Harish'?

The above is only an example of the requiment. The below is the simulated data with same structure for my use case:

print(type(tup_obj))
print(tup_obj)

<class 'tuple'>
(101010, 5, 1, 200, 'text', 'text', 'text', 'text', 18075, 'text', 'text', 'text', 37, 2, 57, 'Y', 'S', 'S', 'text', 123, 'text')

Upvotes: 2

Views: 2649

Answers (1)

Vishnudev Krishnadas
Vishnudev Krishnadas

Reputation: 10970

Use a dictionary data structure to store and access like tup_obj.name

columns = ['name', 'age', 'height', 'weight']
data = [('a', 20, 170, 80)] # This will be your select query results
out = [dict(zip(columns, x)) for x in data]

Output

[{'name': 'a', 'age': 20, 'height': 170, 'weight': 80}]

Access using

out[0]['name'] # This will give string 'a'

EDIT:

For tuple, use directly

dict(zip(columns, data))

Upvotes: 2

Related Questions