使用 python 3 创建字典时出现值错误

Value Error when creating dictionary using python 3

我是一名完全不熟悉 python 的学生,目前正在学习它。 所以我被要求从一个列表创建一个字典,该列表包含文件中的一行数据。

record = ["Name", "ID", "datetime", "Type", "Lat", "Long", "Central Pressure", "Mean radius gf wind", "Max wind speed", "Comment"]

该文件已经导入,它包含与 record 列表中的元素相对应的多行数据。

数据示例:

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

所以我的代码就像

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

我期待得到

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}

但我收到一条错误消息:

int((record[2])[8:10]),int((record[2])[10:13]), float(record[6]), float(record[7]),
ValueError: could not convert string to float: 

而且如果数据中有一个空白值(例如上面 r2 的例子),它应该被排除在字典之外,我应该怎么做才能实现这一点?

感谢您的帮助!

错误在

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

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

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

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

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

尝试实现这个

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)