1

So the result of this code is a list like this:

[
  {'id': 'abcd', 'created_time': '2016-12-09T13:45:43+0000'},
  {'id': 'efgh', 'created_time': '2016-12-09T07:47:54+0000'},
  {'id': 'ijkl', 'created_time': '2016-12-09T07:47:34+0000'},
  {'id': 'mnop', 'created_time': '2016-12-09T07:47:09+0000'},
  {'id': 'qrst', 'created_time': '2016-12-09T07:46:52+0000'}
]]

and I'd like to get a list like:

ID
abcd
efgh
ijkl
mnop
qrst

I's appreciate any help as I'm pulling my hair out with this one!

def d_values(d, depth):
    if depth == 1:
        for i in d.values():
            yield i
    else:
        for v in d.values():
            if isinstance(v, dict):
                for i in d_values(v, depth-1):
                        yield i


def Get_Message_IDs (Conversation_ID, Token):

    Request = urllib.request.urlopen('https://graph.facebook.com/v2.8/' + Conversation_ID +'?fields=messages&access_token=' + Token)
    ids = list()
    try:
        response = Request
        output = response.read()
        output = output.decode("utf-8")
        output = ast.literal_eval(output)
        output = list(d_values(output, 2))
        print(output)
    except Exception as exec:
        print(exec)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
Harry
  • 11
  • 1
  • Hi, take a look at whta is json format. You could use json.load from json module to convert directly your facebook reponse to a dict. You can then access it like a dictionnary and iter in it. – iFlo Dec 09 '16 at 14:08
  • 1
    Indeed, using ast.literal_eval seems naive :-). http://stackoverflow.com/questions/9949533/python-eval-vs-ast-literal-eval-vs-json-decode#9949553 – JoshRomRock Dec 09 '16 at 14:18

3 Answers3

1

Assuming the extra ] at the end is a typo, your list looks like a json list. So just parse it using the appropriate json module (yes JSON syntax and Python dict and list are very much look-alike):

import json
# response is the result of your urllib.requests.urlopen(...)
my_list = json.loads(response.read().decode('utf-8'))

# then you can access the ids 
print([element['id'] for element in my_list)

# if you want the exact output you mentioned in your question:
print("ID")
for element in my_list:
    print(element.get('id'))

And BTW I would suggest that you use the external requests library instead of the built-in urllib, it will take all the pain of parsing JSON responses away from you:

import requests
response = requests.get('https://graph.facebook.com/v2.8/' + Conversation_ID +'?fields=messages&access_token=' + Token)
print(response.json())
Guillaume
  • 5,497
  • 3
  • 24
  • 42
0

Try :

l = # your list of dictionaries (json format)
[dic['id'] for dic in l]

Output :

['abcd', 'efgh', 'ijkl', 'mnop', 'qrst']
MMF
  • 5,750
  • 3
  • 16
  • 20
0
ls = [
        {'id': 'abcd', 'created_time': '2016-12-09T13:45:43+0000'},
        {'id': 'efgh', 'created_time': '2016-12-09T07:47:54+0000'},
        {'id': 'ijkl', 'created_time': '2016-12-09T07:47:34+0000'},
        {'id': 'mnop', 'created_time': '2016-12-09T07:47:09+0000'},
        {'id': 'qrst', 'created_time': '2016-12-09T07:46:52+0000'}
      ]
for d in ls:
  if isinstance(d, dict):
    print d['id']

abcd
efgh
ijkl
mnop
qrst
Ari Gold
  • 1,528
  • 11
  • 18