-4

I need some assistance in accessing the values or entries inside the Tuple.

Consider the following data:

('{"email": "test@example.com"}',)
<type 'tuple'>
{"email": "test@example.com"}
<type 'str'>

First it was basically a Tuple.

After further processing, it is being converted to "str:.

You can have a look at following code.

So here I need to get the value for "email".

I tried the following code

rows = cur.fetchall()

for row in rows:
    print  row
    print type(row)

    for rowitems in row:

            print rowitems
            print type(rowitems)

Some one let me the way for the same.

iamnewuser
  • 360
  • 1
  • 4
  • 16
  • 1
    What does your code have to do with the question? What is your input and what is your desired output? Please provide a complete, unambiguous example of what you want. – timgeb Feb 01 '16 at 07:31
  • @timgeb it looks like he needs to convert the tuple values from a string to a dictionary. I'm on my phone, but I would think he wants `dict(row[0])['email']` – Jason Feb 01 '16 at 07:34
  • Do you know how that string was created in the first place? At first glance it **could** be JSON, but there isn't really all that much context here. – Martijn Pieters Feb 01 '16 at 07:34
  • @Jason: no, `dict()` does not take a string value like that. – Martijn Pieters Feb 01 '16 at 07:34
  • As for the tuple, that's because you are running a SQL query; results are *always* a sequence of columns. Your query here has only one column, hence a tuple with just one value. – Martijn Pieters Feb 01 '16 at 07:35
  • @Martijn thanks. I wasn't sure and I couldn't test it real quick. I know Python can easily convert some types. – Jason Feb 01 '16 at 07:36
  • My second guess would have been `eval`. http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary – Jason Feb 01 '16 at 07:39

1 Answers1

3

You can use json.loads to parse the string to JSON.

Example

>>> import json
>>> a_tuple = ('{"email": "test@example.com"}',)
>>> [ json.loads(value)['email'] for value in a_tuple ]
[u'test@example.com']

This will give you a list of emails from the a_tuple

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
  • Here's another alternative: http://stackoverflow.com/questions/988228/converting-a-string-to-dictionary – Jason Feb 01 '16 at 07:41
  • 3
    @Jason: note the quotes contained in that string and in this one. The double quotes make this far more likely to be JSON. If Python's `repr()` output had been recorded in the database, then single quotes would have been generated and that other post would have absolutely applied. – Martijn Pieters Feb 01 '16 at 07:44
  • @Martijn would it matter in this case? `literal_eval` would covert it since it is a valid Python command? – Jason Feb 01 '16 at 07:55
  • 1
    @Jason: if this is JSON, then yes, it does matter, because as soon as there are boolean values, nulls or non-ASCII code-points in the string encoded to `\uhhhh` escapes, then `ast.literal_eval()` will either fail or produce invalid results. – Martijn Pieters Feb 01 '16 at 08:10