from data import LocationData field_col_name = { 'lat' : '纬度', 'lon' : '经度', 'eig' : '精度估算', 'prec' : "误差", } def data_from_row(d, yaw_col): d1 = {} for field in field_col_name: d1[field] = float(d[field_col_name[field]]) if yaw_col is None: d1['yaw'] = 0 else: d1['yaw'] = d[yaw_col] if d1['lat'] == 0: d1['lat'] = None if d1['lon'] == 0: d1['lon'] = None return LocationData(**d1) def parse_data_from_csv(f, yaw_col = None): if type(f) == str: with open(f, encoding='gb2312') as fd: return parse_data_from_csv(fd) from csv import DictReader rd = DictReader(f) ret = [] for row in rd: ret.append(data_from_row(row, yaw_col)) return ret if __name__ == '__main__': print(parse_data_from_csv("test.csv"))