I am a student who is totally new to python and is currently learning it. So i was asked to create a dictionary from a list which contains a row of data in the file.
record = ["Name", "ID", "datetime", "Type", "Lat", "Long", "Central Pressure", "Mean radius gf wind", "Max wind speed", "Comment"]
the file is already imported and it consists of many rows of data that correspond to the the element in record
list.
Example of data:
r1 = ["unnamed", "AU190607_01U", "1907-01-17 23:00", "T", "-13", "146.5", "994", "100", "10.3"]
r2 = ["unnamed", "AU190607_01U", "1907-01-17 23:00", "T", "-13", "146.5", "994", "", "10.3"]
so my code is like
def parse_record(record):
key = ["id", "name", "year", "month", "day", "hour", "central pressure",
"radius", "speed", "lat", "long"]
value = [str(record[1]), str(record[0]), int((record[2][:4]),
int((record[2])[5:7]),int((record[2])[8:10]),
int((record[2][10:13]),float(record[6]),
float(record[7]),float(record[8]),float(record[4]), float(record[5])]
record_dictionary = dict(zip(key,value))
return record_dictionary
I am expecting to get
a1 = {"id": "AU190607_01U", "name": "unnamed", "year": 1907, "month": 1,
"day": 17, "hour": 23,"central pressure": 994.0, "radius": 100.0,
"speed": 10.3, "lat": -13.0, "long": 146.5}
but I get an error message says:
int((record[2])[8:10]),int((record[2])[10:13]), float(record[6]), float(record[7]),
ValueError: could not convert string to float:
and also if there is a blank value in data(such as in eg of r2 above), it is supposed to be left out of dictionary, what should i do to achieve this?
Thanks for any help in this!