I have a list of values as below:
dataPoint1 = ["unnamed", "A0001", "2009-04-01 00:01", "HT","146.5", "146.75", "FromTestDevice0001", "181"]
Format of this list is
dataHeader = [Name, ID, DateTime, TypeOfDevice, Reading1, Reading2, Comment, Max Value]
I need to put these value into a dictionary in the following format; "id" (string), "name" (string), "year" (int), "month" (int), "day" (int), "hour" (int), "reading1" (float), "reading2" (float), "max" (float). Intended output:
{"id": "A0001", "name": "unnamed", "year": 2009, "month": 04, "day": 01, "hour": 00, "reading1": 146.5, "reading2": 146.75, "max": 181}
These are fixed outputs (e.g. dataPoint1) in the order of 'dataHeader'. How to map these value to a dictionary in the given format?
The code that I run currently is as below. Is there an alternative way that I can do this? Can we optimise this in time or memory-wise? zip function didn't work (for me) as the dataPoints and mappings are not in order and due to type conversion.
def parse_data(dataPoint):
"This function maps dataPoints to a dictionary"
data_dictionary = dict()
if dataPoint[1] != '':
data_dictionary['id'] = dataPoint[1]
if dataPoint[0] != '':
data_dictionary['name'] = dataPoint[0]
if dataPoint[2] != '':
data_dictionary['year'] = int(dataPoint[2][:4])
data_dictionary['month'] = int(dataPoint[2][5:7])
data_dictionary['day'] = int(dataPoint[2][8:10])
data_dictionary['hour'] = int(dataPoint[2][11:13])
if dataPoint[4] != '':
data_dictionary['reading1'] = float(dataPoint[4])
if dataPoint[5] != '':
data_dictionary['reading2'] = float(dataPoint[5])
if dataPoint[7] != '':
data_dictionary['max'] = float(dataPoint[7])
return data_dictionary