I’m trying to decipher what appears to be a dictionary in a data file for a colleague.
I’ve managed to read the Excel file into a dictionary using:
import pandas as pd
wordList = pd.read_excel('DM_ALLJune2020short.xlsx', sheet_name='DM_ALLJune2020')
rows=len(wordList)
Most of the Excel file is simple, but one column has entries like this:
{"2019-01-01":{"drinks":[{"type":"Champagne","name":"Flute","size":"150ml","vol":"150","css":"champagne-flute-150","abv":"12","qty":"1"}]},"2019-01-02":{"drinks":[{"type":"Beer\/Lager\/Ale","name":"Schooner","size":"425ml","vol":"425","css":"beer-schooner-425","abv":"3.5","qty":"4"}]}}
and
{"2019-01-16":{"spend":"23"},"2019-01-15":{"drinks":[{"type":"Beer\/Lager\/Ale","name":"Schooner","size":"425ml","vol":"425","css":"beer-schooner-425","abv":"3.5","qty":"1"}]}}
In principle what I’d like to do is loop over the wordList variable and extract the date and other data so I can save it into a more usable format.
However, as soon as I try alcometer=wordList[trials.thisN][‘Alcoholbreakdown’] or alcometer=wordList[trials.thisN+1][‘Alcoholbreakdown’] (in case dictionaries start at index 1)
it gives an error
Traceback (most recent call last):
File "C:\Program Files\PsychoPy3\lib\site-packages\pandas\core\indexes\base.py", line 3078, in get_loc
return self._engine.get_loc(key)
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\p0071480\Documents\data\E2020\alcometer_lastrun.py", line 227, in <module>
alcometer=wordList[trials.thisN+1]['Alcoholbreakdown']
File "C:\Program Files\PsychoPy3\lib\site-packages\pandas\core\frame.py", line 2688, in __getitem__
return self._getitem_column(key)
File "C:\Program Files\PsychoPy3\lib\site-packages\pandas\core\frame.py", line 2695, in _getitem_column
return self._get_item_cache(key)
File "C:\Program Files\PsychoPy3\lib\site-packages\pandas\core\generic.py", line 2489, in _get_item_cache
values = self._data.get(item)
File "C:\Program Files\PsychoPy3\lib\site-packages\pandas\core\internals.py", line 4115, in get
loc = self.items.get_loc(item)
File "C:\Program Files\PsychoPy3\lib\site-packages\pandas\core\indexes\base.py", line 3080, in get_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 1
##### Experiment ended. #####