HELP! Micropython dictionary

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
mikruth
Posts: 18
Joined: Wed Jan 11, 2017 6:00 pm

HELP! Micropython dictionary

Post by mikruth » Wed Jan 11, 2017 6:17 pm

I have been searching docs for 2 days and can't find the info I require. I have a dictionary named data, containing info from dweet.io. I wan't to extract
{'Humidity': 72, 'Temperature': 17, 'Battery': 4.10} from the following dictionary...

{'the': 'dweets', 'with': [{'content': {'Humidity': 72, 'Temperature': 17,
'Battery': 4.10}, 'created': '2017-01-11T17:30:00.332Z',
'thing': '111111111111'}], 'by': 'getting', 'this': 'succeeded'}

I would be much obliged if someone can tell me how to extract this or point me in the right direction. Standard dictionary is OK but it is the embedded parts I can't work out.
Thanks.

User avatar
dhylands
Posts: 2503
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: HELP! Micropython dictionary

Post by dhylands » Wed Jan 11, 2017 8:26 pm

As far as I can tell, there isn't anything MicroPython specific about your question. I did the following (and included the intermediate steps):

Code: Select all

MicroPython v1.8.6-205-g91359c8-dirty on 2016-12-19; PYBv1.0 with STM32F405RG
Type "help()" for more information.
>>> 
>>> x = {'the': 'dweets', 'with': [{'content': {'Humidity': 72, 'Temperature': 17,
... 'Battery': 4.10}, 'created': '2017-01-11T17:30:00.332Z',
... 'thing': '111111111111'}], 'by': 'getting', 'this': 'succeeded'}
>>> x
{'the': 'dweets', 'this': 'succeeded', 'by': 'getting', 'with': [{'created': '2017-01-11T17:30:00.332Z', 'thing': '111111111111', 'content': {'Battery': 4.1, 'Humidity': 72, 'Temperature': 17}}]}
>>> x['with']
[{'created': '2017-01-11T17:30:00.332Z', 'thing': '111111111111', 'content': {'Battery': 4.1, 'Humidity': 72, 'Temperature': 17}}]
>>> x['with'][0]
{'created': '2017-01-11T17:30:00.332Z', 'thing': '111111111111', 'content': {'Battery': 4.1, 'Humidity': 72, 'Temperature': 17}}
>>> x['with'][0]['content']
{'Battery': 4.1, 'Humidity': 72, 'Temperature': 17}
>>> x['with'][0]['content']['Battery']
4.1
>>> x['with'][0]['content']['Humidity']
72
>>> x['with'][0]['content']['Temperature']
17
>>> 
and it works exactly the same way in regular python.

mikruth
Posts: 18
Joined: Wed Jan 11, 2017 6:00 pm

Re: HELP! Micropython dictionary

Post by mikruth » Wed Jan 11, 2017 9:42 pm

Thanks very much for that. I would never have got there with google. I am new to python and have only coded with Arduino.
Thanks again......Mick

User avatar
deshipu
Posts: 1221
Joined: Thu May 28, 2015 5:54 pm

Re: HELP! Micropython dictionary

Post by deshipu » Thu Jan 12, 2017 9:03 am

You might want to try this first: https://docs.python.org/3/tutorial/

It should make your adventure with Python much smoother.

Post Reply