Solution for Can’t extract data from list of dictionaries, ‘NoneType’ object is not subscript
is Given Below:
I have a pickle file AQM_kath.pkl
that is two sensor locations and their data in a dictionary.
len(AQM_kath)
>>> 2
len(AQM_kath[0])
>>> 1011
len(AQM_kath[1])
>>> 1026
Each of the entries in the respective lists is a dictionary entry (sensor 1 has 1011 entries, sensor 2 has 1026) with several keys. I am trying to run through both sensor locations (AQM_kath[0] and AQM_kath[1]) and compile all of the data entries of a specific key into 1 list. The included keys are Image
, Date
, Time
, and Temp
. Here is what i have tried so far for temperature, my end goal is a 2037 length list of temperatures:
import pickle as pkl
import numpy as np
temp =[]
with open('/work/AQM_kath.pkl', 'rb') as fp:
for station in tqdm(pkl.load(fp)):
for data_point in station:
temp.append(data_point['Temp'])
However i get the error
TypeError Traceback (most recent call last)
<ipython-input-68-41f4e4b6f854> in <module>
3 for station in tqdm(pkl.load(fp), position=0, leave=True):
4 for data_point in station:
----> 5 temp.append(data_point['Temp'])
TypeError: 'NoneType' object is not subscriptable
It works for the first 10 entries, but then spits out this error. Not sure what i’m missing here, any help appreciated!
You say
Each of the entries in the respective lists is a dictionary entry
but this is apparently wrong. There are also some None
entries mixed into the lists. Assuming this is normal, you can skip them.
with open('/work/AQM_kath.pkl', 'rb') as fp:
for station in tqdm(pkl.load(fp)):
for data_point in station:
if data_point:
temp.append(data_point['Temp'])
If this is not expected, you should figure out why incomplete data is being written to the file in the first place and fix that.