【问题标题】:Convert json list into dictionary in python在python中将json列表转换为字典
【发布时间】:2017-07-02 06:03:22
【问题描述】:

我使用的数据是 Twitter API 的 Twitter 趋势主题。

url_0 = 'https://api.twitter.com/1.1/trends/place.json?id=2459115'  
res = requests.get(url_0, auth=auth)  

print(res, res.status_code, res.headers['content-type'])  
print(res.url)

top_trends_twitter = res.json()  
data= top_trends_twitter[0]  

这是数据的样子:

[{'as_of': '2017-02-13T21:59:32Z',  
  'created_at': '2017-02-13T21:53:22Z',  
  'locations': [{'name': 'New York', 'woeid': 2459115}],  
  'trends': [{'name': 'Victor Cruz',  
    'promoted_content': None,  
    'query': '%22Victor+Cruz%22',  
    'tweet_volume': 45690,  
    'url': 'http://twitter.com/search?q=%22Victor+Cruz%22'},  
   {'name': '#percussion',  
    'promoted_content': None,  
    'query': '%23percussion',  
    'tweet_volume': None,  
    'url': 'http://twitter.com/search?q=%23percussion'},  .....etc

现在,我用 SQL 连接服务器并创建数据库和表后,出现错误。这是给我带来麻烦的部分:


for entry in data:  
    trendname = entry['trends']['name']  
    url = entry['trends']['url']  
    num_tweets = entry['trends']['trend_volume']  
    date= entry['as_of']  
    print("Inserting trend", trendname, "at", url)  
    query_parameters = (trendname, url, num_tweets, date)  
    cursor.execute(query_template, query_parameters) 



con.commit()  
cursor.close()  

然后,我得到这个错误:


TypeError                                 Traceback (most recent call last)
<ipython-input-112-da3e17aadce0> in <module>()  
     29   
     30 for entry in data:  
---> 31     trendname = entry['trends']['name']  
     32     url = entry['trends']['url']  
     33     num_tweets = entry['trends']['trend_volume']  

TypeError:字符串索引必须是整数

如何将字符串集放入字典中,以便将其用于输入数据代码?

【问题讨论】:

    标签: python json api dictionary


    【解决方案1】:

    您需要entry['trends'][0]['name']entry['trends'] 是一个列表,您需要整数索引来访问列表中的项目。

    这样试试:

    data=[{'as_of': '2017-02-13T21:59:32Z',  
      'created_at': '2017-02-13T21:53:22Z',  
      'locations': [{'name': 'New York', 'woeid': 2459115}],  
      'trends': [{'name': 'Victor Cruz',  
        'promoted_content': None,  
        'query': '%22Victor+Cruz%22',  
        'tweet_volume': 45690,  
        'url': 'http://twitter.com/search?q=%22Victor+Cruz%22'},  
       {'name': '#percussion',  
        'promoted_content': None,  
        'query': '%23percussion',  
        'tweet_volume': None,  
        'url': 'http://twitter.com/search?q=%23percussion'}]}]
    
    for entry in data:
        date= entry['as_of']
        for trend in entry['trends']:
            trendname = trend['name']  
            url = trend['url']  
            num_tweets = trend['tweet_volume']  
            print trendname, url, num_tweets, date
    

    输出:

    Victor Cruz http://twitter.com/search?q=%22Victor+Cruz%22 45690 2017-02-13T21:59:32Z
    #percussion http://twitter.com/search?q=%23percussion None 2017-02-13T21:59:32Z
    

    【讨论】:

    • 仍然遇到同样的问题...当我执行 data['as_of'] 时,我确实得到了一个值,但是当我运行该代码时,我得到指向 date=entry[ 的相同错误'as_of']
    • @Elizabeth 代码已经过测试,它适用于您在问题中提供的数据。我认为你应该试试data = res.json()。我希望它会起作用。
    猜你喜欢
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 2014-07-28
    • 2016-10-20
    • 2022-01-02
    • 1970-01-01
    相关资源
    最近更新 更多