簡體   English   中英

如何從dweet打印特定值?

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

我從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')

題 :

如何從dweet輸出上方單獨打印mouse_x值的值?

我試過的是:

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'}

如何為mouse_x打印值271?

對於你的內容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})

這將創建一個dweet與名my_thing_name 現在,當您使用dweepy.get_latest_dweet_for()查詢dweet時,將返回一個字典,如下所示

a = dweepy.get_latest_dweet_for('my_thing_name')

結果是一個object列表,其鍵是contentthing和已created 要檢索鍵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'] ,它將返回值271 但是,這僅適用於第一個dweet對象。 如果返回了多個對象,則可以遍歷項目並使用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