2

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!

4

3 回答 3

2

错误在于

value = […, float(record[7])…]

由于在您的第二个值列表中,record[7]is""这不是有效的浮点文字。

于 2017-08-23T14:34:52.460 回答
1

尝试实现这个

def parse_record(record):
    key = ["id", "name", "year", "month", "day", "hour", "central pressure",
           "radius", "speed", "lat", "long"]
    record = [i if i else 0 for i in record]
    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

record = ["unnamed", "AU190607_01U", "1907-01-17 23:00", "T", "-13", "146.5", "994", "", "10.3"]        

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"]  

parse_record(r1)
parse_record(r2)

请注意,在第二条记录的输出中,您的值将为零radius。这是因为此记录不存在任何价值。

编辑

我已经做出了改变。

def parse_record(record):
    key = ["id", "name", "year", "month", "day", "hour", "central pressure",
           "radius", "speed", "lat", "long"]
    record = [i if i else 0 for i in record]

    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])]
    _ = [(a,b) for a,b in zip(key,value) if b]
    record_dictionary = dict(_)

    return record_dictionary

record = ["unnamed", "AU190607_01U", "1907-01-17 23:00", "T", "-13", "146.5", "994", "", "10.3"]        

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"]  

parse_record(r1)
parse_record(r2)
于 2017-08-23T18:29:29.850 回答
0

这是帮助您处理空白值的方法(在制作字典时不更改类型)。

record_dictionary = {a:b for a,b in zip(key,value) if b != '' }

上面有一个很好的建议,首先由@tdube 压缩键和值,然后才更改那里的类型。

于 2017-08-23T14:50:08.487 回答