1.CSV文件:
CSV(Comma-Separated Values,逗号分隔的值)是一种简单、实用的文件格式,用于存储和表示包括文本、数值等各种类型的数据。CSV 文件通常以 .csv 作为文件扩展名。这种文件格式的一个显著特点是:文件内的数据以逗号 , 分隔,呈现一个表格形式。CSV 文件已广泛应用于存储、传输和编辑数据。
2.JSON文件:
JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)
JSON是轻量级的文本数据交换格式 JSON 独立于语言:JSON 使用 Javascript语法来描述数据对象,但是 JSON 仍然独立于语言和平台。JSON 解析器和 JSON 库支持许多不同的编程语言。 目前非常多的动态(PHP,JSP,.NET)编程语言都支持JSON。
3.转换实例:
csv文件:
转换成json文件后:
代码:
import json
f = open("D:/文件/资料/GT.csv", "r", encoding='GB2312') # csv文件的路径
data_lines = f.readlines()
f.close()
data_lines.pop(0)
values = []
for line in data_lines:
line = line.replace("\n", "")
values.append(line.split(","))
# print(ls)
# json文件为键值对,keys为左侧键
keys = ["stamp_sec", "obj_stamp_sec", "frame_num", "source", "id", "track_id", "lane_id", "center_x", "center_y", "center_z", "closest_point_x",
"closest_point_y", "closest_point_z", "closest_box_x", "closest_box_y", "closest_box_z", "front_bumper_x",
"front_bumper_y", "front_bumper_z", "rear_bumper_x", "rear_bumper_y", "rear_bumper_z", "move_status", "cut_in", "cut_out",
"cipv", "velocity_x", "velocity_y", "velocity_z", "project_velocity_x", "project_velocity_y", "project_velocity_z",
"acceleration_x", "acceleration_y", "acceleration_z", "project_acceleration_x", "project_acceleration_y",
"project_acceleration_z", "angular_velocity", "obj_yaw", "direction_x", "direction_y", "direction_z", "height", "length",
"width", "is_radar_matching", "is_tracked", "radar_velocity_x", "radar_velocity_y", "radar_velocity_z", "type",
"type_confidence", "pose_pos_x", "pose_pos_y", "pose_pos_z", "roll", "pitch", "yaw", "car_twist", "car_acceleration",
"reserve_score", "reserve_info", "anchor_x", "anchor_y", "anchor_z", "lidar_name"]
fw = open("D:/文件/资料/a.json", "w", encoding='utf-8') # 创建json文件的路径
# 利用for循环遍历,形成键值对
dict_re = [dict(zip(keys, row)) for row in values] if values else None
# print(dict_re)
a = json.dumps(dict_re, indent=4, ensure_ascii=False)
print(a)
fw.write(a)
fw.close()