2017-07-28 126 views
0

列表無法循環我有一個字典如下:通過快譯通的蟒蛇

{ u'has_more': False, 
    u'is_limited': True, 
    u'latest': u'1501149118.071555', 
    u'messages': [ { u'text': u'--Sharp 1.9 DM\n--Modifying and testing DM script for bypassing existing sonumber validation and add line items', 
         u'ts': u'1501149054.047400', 
         u'type': u'message', 
         u'user': u'U0HN06ZB9'}, 
        { u'text': u'-- support to engineering on Licensing infra upgrade to 3.6\n - created a new key for qa on current 3.5 ubuntu 12 instance\n - added that key to the instance , created the ami and shared it with QA\n - short discussion with Navin on same', 
         u'ts': u'1501148934.002719', 
         u'type': u'message', 
         u'user': u'U02RRQJG1'}, 
        { u'inviter': u'U03FE3Z7D', 
         u'subtype': u'channel_join', 
         u'text': u'<@U0HN06ZB9|shikhar.rastogi> has joined the channel', 
         u'ts': u'1501148921.998107', 
         u'type': u'message', 
         u'user': u'U0HN06ZB9'}, 
        { u'inviter': u'U03FE3Z7D', 
         u'subtype': u'channel_join', 
         u'text': u'<@U02RRQJG1|himani> has joined the channel', 
         u'ts': u'1501148328.777625', 
         u'type': u'message', 
         u'user': u'U02RRQJG1'}, 
        { u'text': u'something like ^^^^', 
         u'ts': u'1501148318.773838', 
         u'type': u'message', 
         u'user': u'U03FE3Z7D'}, 
        { u'text': u'-- This is test \n-- Not\n-- test1\n-- Test b', 
         u'ts': u'1501148309.770614', 
         u'type': u'message', 
         u'user': u'U03FE3Z7D'}, 
        { u'text': u'<!channel> can all of you start putting some random crap in same format as shift handoff', 
         u'ts': u'1501148287.762336', 
         u'type': u'message', 
         u'user': u'U03FE3Z7D'}, 
        { u'text': u'<!channel> can all of you start putting some random crap in same format as shift handoff', 
         u'ts': u'1501148287.762161', 
         u'type': u'message', 
         u'user': u'U03FE3Z7D'}, 
        { u'text': u'sjvnsv', 
         u'ts': u'1501138569.469475', 
         u'type': u'message', 
         u'user': u'U03FE3Z7D'}, 
        { u'text': u'-- Test1 \n-- Leave this ASAP', 
         u'ts': u'1501136157.933720', 
         u'type': u'message', 
         u'user': u'U03FE3Z7D'}, 
        { u'bot_id': u'B19LZG1A5', 
         u'subtype': u'bot_message', 
         u'text': u'This is crazy', 
         u'ts': u'1501075281.418010', 
         u'type': u'message', 
         u'username': u'TEST_BOT'}], 
    u'ok': True, 
    u'oldest': u'1500820472.964970'} 

我現在想提取2個東西一日是user及其相應text,但不知何故,我不能夠得到這個使用下列內容:

json_objects = json.loads(r.text) 
for i in json_objects: 
    print json_objects['messages'][i]['user'] 
    print json_objects['messages'][i]['text'] 

以上拋出一個錯誤:

Traceback (most recent call last): 
    File "clean_test.py", line 45, in <module> 
    get_channel_messages() 
    File "clean_test.py", line 38, in get_channel_messages 
    print json_objects['messages'][i]['user'] 
TypeError: list indices must be integers, not unicode 

上面實際上應該得到user並調用user_detail()meathod得到的名稱和回來,一旦做到這一點,我想要的內容以下列方式

username1: 
    -- text 
username2: 
    -- text2 
+0

我想你遍歷錯誤迭代。在json_objects [「消息」]試着去'因爲我:' – nutmeg64

回答

4

你要遍歷表的索引,而不是遍歷外字典

json_objects = json.loads(r.text) 
for i in range(len(json_objects['messages'])): 
    print json_objects['messages'][i]['user'] 
    print json_objects['messages'][i]['text'] 

或者另一種方式是(在Python的方式)的鍵:

for i in json_objects['messages']: 
    print i['user'] 
    print i['text'] 
+0

這裏的第二種方法是迭代迭代集合中的項目(而不是遍歷它們的索引),這是pythonic解決方案。 –

+0

是的,我這樣做是因爲OP似乎在做另一種方式,所以我糾正了這一點。我應該在答案中加上這一點。謝謝 – anon

0

您轉儲到一個文件正在迭代字典並嘗試訪問列表。嘗試用這種

for i in json_objects['messages']: 
    print i['user'] 
    print i['text'] 
+0

這不起作用,見^^匿名雁 – Kittystone

+0

@Kittystone,燁答應了,我誤寫成「json_objects [」消息「]」打印報表。) –