简体   繁体   English

如何从dweet打印特定值?

[英]How to print a particular value from dweet?

I have an output from dweepy as : 我从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'}]

My input was : 我的输入是:

dweepy.get_latest_dweet_for('my_thing_name')

Question : 题 :

How to print value of mouse_x value alone from above dweet output ? 如何从dweet输出上方单独打印mouse_x值的值?

What i tried was : 我试过的是:

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

which gave me output : 这给了我输出:

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

How can i print value 271 for mouse_x ? 如何为mouse_x打印值271?

For the content you are dweeting with the name as my_thing_name , let us assume that you have dweet_for() as follows initially 对于你的内容dweetingnamemy_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})

Which creates a dweet with name as my_thing_name . 这将创建一个dweet与名my_thing_name Now when you query for the dweet using dweepy.get_latest_dweet_for() you are returned a dictionary as follows 现在,当您使用dweepy.get_latest_dweet_for()查询dweet时,将返回一个字典,如下所示

a = dweepy.get_latest_dweet_for('my_thing_name')

Result is a list of object whose keys are content , thing and created . 结果是一个object列表,其键是contentthing和已created For retrieving the content for key mouse_x you need to access the object inside the dweet response's content dictionary. 要检索键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'}]

This can be done by doing a[0]['content']['mouse_x'] which will return the value 271 . 这可以通过执行a[0]['content']['mouse_x'] ,它将返回值271 This will however work only with the first dweet object. 但是,这仅适用于第一个dweet对象。 If there are multiple objects returned you can loop through the items and access the mouse_x value by using a[i]['content']['mouse_x'] where i corresponds to the index of the items. 如果返回了多个对象,则可以遍历项目并使用a[i]['content']['mouse_x']访问mouse_x值,其中i对应于项目的索引。

>>> a[0]['content']['mouse_x']
271

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM