Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

Python - IndexError: list index out of range

Why would data['entities']['urls'][0]['expanded_url'] would produce 'IndexError: list index out of range' error? I understand what this error means but can't see why? perhaps too sleepy at 2 am? Please help

data = {
u'entities': {
    u'symbols': [], 
    u'user_mentions': [],
    u'hashtags': [{u'indices': [3, 13], u'text': u'firstpost'},
                  {u'indices': [22, 35], u'text': u'snowinginnyc'}],
    u'urls': [{u'url': u'https://t.co/0sClwIMXKW', 
               u'indices': [36, 59],
               u'expanded_url': u'https://vine.co/v/hQPlQ9l5XDD',
               u'display_url': u'vine.co/v/hQPlQ9l5XDD'}
             ]
}},                                                                     
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is not a valid python code, u are missing braces. Here :

data = {
u'entities': {
    u'symbols': [], 
    u'user_mentions': [],
    u'hashtags': [{u'indices': [3, 13], u'text': u'firstpost'},
                  {u'indices': [22, 35], u'text': u'snowinginnyc'}],
    u'urls': [{u'url': u'https://t.co/0sClwIMXKW', 
               u'indices': [36, 59],
               u'expanded_url': u'https://vine.co/v/hQPlQ9l5XDD',
               u'display_url': u'vine.co/v/hQPlQ9l5XDD'}
             ]
}
}

and print data['entities']['urls'][0]['expanded_url']

prints vine.co/v/hQPlQ9l5XDD


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...