2

I'm attempting to retrieve information from a postgreSQL database, a single column to be specific within which the data is stored in a 2D array format

example :

('user1', 'user2,'user3'),('user1', 'user2,'user3'),('user1', 'user2,'user3')

I'm extracting the values into a values_list using django's database abstraction interface

q = DBReports.objects.all().filter(name__contains = name1)  
q = (q.values_list(columnName))

This then places all the values into a values_list, from this I need to extract two of the values from each list within the list, e.g user1 and user3 which are at array positions x,0,x,2 respectively, I am unable to determine how many lists within lists there will be, therefore I must iterate through the length

However I cannot convert the values_list to a 2D list, the following does not allow the code to be iteratable

list((q.values_list(columnName)))

And just returns a length of one, and all values in a single list.

Is there a way i can adequately extract this 2D list and place it into a 2D list within python?

Printed values are below (ignore all the '' its used later in the program)

[("[('user1', 'user2', 'user3', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), ('user4', 'user5', 'user6', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), ('user1', 'user3', 'user6', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), ('user1', 'user2', 'user7', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), ('user1', 'user2', 'user2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')]",)]
2
  • print q.values_list(columnName) for details. Commented Aug 17, 2015 at 11:47
  • @RajeshKaushik Thanks, See above Commented Aug 17, 2015 at 11:54

1 Answer 1

1

You can get it done like this

import ast
q = DBReports.objects.all().filter(name__contains = name1)  
q = (q.values_list(columnName))
users_list = ast.literal_eval(q[0][0])

Check the users_list value

print users_list
[('user1', 'user2', 'user3', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), ('user4', 'user5', 'user6', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), ('user1', 'user3', 'user6', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), ('user1', 'user2', 'user7', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ''), ('user1', 'user2', 'user2', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '')]

This will enable iteration on users_list.

Note that ast.eval is safe to use. For more details read the docs here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.