Python如何將CSV文件轉(zhuǎn)JSON文件
1.CSV文件:
CSV(Comma-Separated Values,逗號分隔的值)是一種簡單、實(shí)用的文件格式,用于存儲和表示包括文本、數(shù)值等各種類型的數(shù)據(jù)。CSV 文件通常以 .csv 作為文件擴(kuò)展名。這種文件格式的一個顯著特點(diǎn)是:文件內(nèi)的數(shù)據(jù)以逗號 , 分隔,呈現(xiàn)一個表格形式。CSV 文件已廣泛應(yīng)用于存儲、傳輸和編輯數(shù)據(jù)。
2.JSON文件:
JSON 指的是 JavaScript 對象表示法(JavaScript Object Notation)
JSON是輕量級的文本數(shù)據(jù)交換格式 JSON 獨(dú)立于語言:JSON 使用 Javascript語法來描述數(shù)據(jù)對象,但是 JSON 仍然獨(dú)立于語言和平臺。JSON 解析器和 JSON 庫支持許多不同的編程語言。 目前非常多的動態(tài)(PHP,JSP,.NET)編程語言都支持JSON。
3.轉(zhuǎn)換實(shí)例:
csv文件:
轉(zhuǎn)換成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為左側(cè)鍵 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') # 創(chuàng)建json文件的路徑 # 利用for循環(huán)遍歷,形成鍵值對 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()
總結(jié)
到此這篇關(guān)于Python如何將CSV文件轉(zhuǎn)JSON文件的文章就介紹到這了,更多相關(guān)Python CSV轉(zhuǎn)JSON文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于matplotlib-legend 位置屬性 loc 使用說明
這篇文章主要介紹了關(guān)于matplotlib-legend 位置屬性 loc 使用說明,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python里使用正則表達(dá)式的組嵌套實(shí)例詳解
這篇文章主要介紹了python里使用正則表達(dá)式的組嵌套實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10tensorflow 大于某個值為1,小于為0的實(shí)例
這篇文章主要介紹了tensorflow 大于某個值為1,小于為0的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06numpy.ndarray.flatten()函數(shù)的具體使用
本文主要介紹了numpy.ndarray.flatten()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03