csv_parser.py 856 B

123456789101112131415161718192021222324252627282930313233
  1. from data import LocationData
  2. field_col_name = {
  3. 'lat' : '纬度', 'lon' : '经度', 'eig' : '精度估算', 'prec' : "误差",
  4. }
  5. def data_from_row(d, yaw_col):
  6. d1 = {}
  7. for field in field_col_name:
  8. d1[field] = float(d[field_col_name[field]])
  9. if yaw_col is None:
  10. d1['yaw'] = 0
  11. else:
  12. d1['yaw'] = d[yaw_col]
  13. if d1['lat'] == 0:
  14. d1['lat'] = None
  15. if d1['lon'] == 0:
  16. d1['lon'] = None
  17. return LocationData(**d1)
  18. def parse_data_from_csv(f, yaw_col = None):
  19. if type(f) == str:
  20. with open(f, encoding='gb2312') as fd:
  21. return parse_data_from_csv(fd)
  22. from csv import DictReader
  23. rd = DictReader(f)
  24. ret = []
  25. for row in rd:
  26. ret.append(data_from_row(row, yaw_col))
  27. return ret
  28. if __name__ == '__main__':
  29. print(parse_data_from_csv("test.csv"))