1

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
Mad
  • 435
  • 2
  • 17

2 Answers2

1

you could do something like that:

import datetime as dt

dataHeader = ['Name', 'ID', 'Year', 'Month', 'Day', 'hour', 'Reading1', 'Reading2', 'MaxValue']
dP1 = ["unnamed", "A0001", "2009-04-01 00:01", "HT","146.5", "146.75", "From TestDevice0001", "181"]
date = dt.datetime.strptime(dP1[2], '%Y-%m-%d %H:%M')
#dP1 = dP1[:2]+[date.year]+[date.month]+[date.day]+[date.hour]+dP1[4:6]+dP1[7:]
dP1 = dP1[:2]+[int(date.year)]+[int(date.month)]+[int(date.day)]+[int(date.hour)]+[float(dP1[4])]+[float(dP1[5])]+[float(dP1[7])]
datadictionary = dict(zip(dataHeader, dP1))
stacktrace
  • 96
  • 1
  • 4
  • I tried this way but missing type conversion here as well. – Mad Sep 08 '20 at 09:27
  • Looks good and concise. Not sure where I will get the optimization (memory or time? ) I assume zip is O(1) and may be better to use instead of multiple if statements. – Mad Sep 08 '20 at 09:55
  • I think zip is O(n), because at least you have to look at each element in the input, but so is your solution. I guess there is not much room for improvements. zip is a build in function, due to that it is probably faster by a linear factor, but not in terms of a time complexity class. – stacktrace Sep 08 '20 at 11:25
0

you can do it like that

data_dictionary=dict(zip(dataHeader,dataPoint1))

the result will be like that:

{'Comment': 'FromTestDevice0001', 'TypeOfDevice': 'HT', 'Name': 'unnamed', 'Reading1': '146.5', 'Reading2': '146.75', 'DateTime': '2009-04-01 00:01', 'Max Value': '181', 'ID': 'A0001'}

it's not exactly as you want, but yu can achieve that by either edit the dataHeader,dataPoint1 , gives separate date/time/year,

or you can do it like that

dataPoint1[2]=[int(dataPoint1[2][:4]),int(dataPoint1[2][5:7]),int(dataPoint1[2][8:10]),int(dataPoint1[2][11:13])]

the result will be like that:

{'Comment': 'FromTestDevice0001', 'TypeOfDevice': 'HT', 'Name': 'unnamed', 'Reading1': '146.5', 'Reading2': '146.75', 'DateTime': [2009, 4, 1, 0], 'Max Value': '181', 'ID': 'A0001'}
HichamDz38
  • 94
  • 7
  • One of the main issues here is the 'type' conversion. Use of 'zip' function will give strings but not int or floats types – Mad Sep 08 '20 at 09:25