【问题标题】:Problems with GET trends/place (Twython)GET 趋势/地点的问题 (Twython)
【发布时间】:2013-06-22 14:54:20
【问题描述】:

试图仅访问多伦多趋势中的“名称”部分。 到目前为止,我有这个,但它给了我错误:

toronto = t.get_place_trends(id=4118)

trend_array = []
for trend in toronto.trends.name:
    trend_array.append(trend) 
    print trend_array
    print trend

在 auth 之后返回整个对象列表,该列表被强制放入一个数组(由于某种原因不能通过索引访问)并作为一个列表返回。

【问题讨论】:

  • 您遇到什么错误?你知道返回值是什么数据类型吗?您可以在第一行之后添加打印类型(toronto)进行确认。
  • but it is giving me errors,您能告诉我们错误吗?不知道问题就解决不了。

标签: python twitter twython


【解决方案1】:

哇,toronto 返回一个必须通过索引访问的列表真是太奇怪了。

这是您需要的代码:

toronto = t.get_place_trends(id=4118)
trend_array = []

if toronto:
    for trend in toronto[0].get('trends', []):
        trend_array.append(trend['name'])
        print trend_array
        print trend

【讨论】:

  • 非常感谢得到我想要的输出我使用了最后一行 print trend['name'] 但这很简单。这种访问信息的方式很奇怪。
猜你喜欢
  • 2016-03-29
  • 2016-01-09
  • 1970-01-01
  • 2021-06-17
  • 2020-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多