0

我有一个来自 dweepy 的输出:

[{'content': {'mouse_x': 271,
   'mouse_y': 285,
   'tilt_x': 0,
   'tilt_y': 0,
   'tilt_z': 82},
  'created': '2018-02-10T06:03:02.680Z',
  'thing': 'my_thing_name'}]

我的输入是:

dweepy.get_latest_dweet_for('my_thing_name')

问题 :

如何mouse_x从上面的 dweet 输出中单独打印值的值?

我尝试的是:

dweepy.dweet_for('my_thing_name', {'': 'mouse_x'})

这给了我输出:

{'content': {'': 'mouse_x'},
 'created': '2018-02-10T06:23:20.320Z',
 'thing': 'my_thing_name',
 'transaction': '6f295639-a667-48ff-bbbf-6dda111333d1'}

我如何打印值 271 mouse_x

4

1 回答 1

0

对于您dweeting使用nameas的内容my_thing_name,让我们假设您dweet_for()最初具有以下内容

dweepy.dweet_for('my_thing_name', {'mouse_x': 271, 'mouse_y': 285, 'tilt_x': 0, 'tilt_y': 0, 'tilt_z': 82})

这会创建一个dweet名称为my_thing_name. 现在,当您查询dweetusingdweepy.get_latest_dweet_for()时,将返回一个字典,如下所示

a = dweepy.get_latest_dweet_for('my_thing_name')

结果是object其键为contentthing的列表created。要检索 key 的内容,mouse_x您需要访问 dweet 响应content字典中的对象。

[{u'content': {u'mouse_y': 285, u'mouse_x': 271, u'tilt_z': 82, u'tilt_x': 0, u'tilt_y': 0}, u'thing': u'my_thing_name', u'created': u'2018-02-10T06:39:53.715Z'}]

这可以通过a[0]['content']['mouse_x']which will return value来完成271。但是,这仅适用于第一个dweet对象。如果返回多个对象,您可以遍历项目并mouse_x通过使用对应于项目索引的a[i]['content']['mouse_x']位置来访问值。i

>>> a[0]['content']['mouse_x']
271
于 2018-02-10T06:47:15.863 回答