Page 1 of 1

HELP! Micropython dictionary

Posted: Wed Jan 11, 2017 6:17 pm
by mikruth
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.

Re: HELP! Micropython dictionary

Posted: Wed Jan 11, 2017 8:26 pm
by dhylands
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.

Re: HELP! Micropython dictionary

Posted: Wed Jan 11, 2017 9:42 pm
by mikruth
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

Re: HELP! Micropython dictionary

Posted: Thu Jan 12, 2017 9:03 am
by deshipu
You might want to try this first: https://docs.python.org/3/tutorial/

It should make your adventure with Python much smoother.