json file content + csv save file [Python]

Example:

import json
import csv

# 从本地的json数据中读取内容信息,


def writecsvdata(ldata):
    headers=['id', 'name', 'pin_yin', 'pin_yin_ad', 'group_id', 'group_code', 'parent_id', 'device_id', 'chan_no', 'gb_id', 'ptz', 'dpi', 'longitude', 'latitude', 'manufacturer', 'model', 'owner', 'block', 'address', 'parental', 'safetyway', 'secrecy', 'position', 'room', 'uses', 'supplylight', 'direction', 'analog_type', 'node_id', 'dropped', 'status']

    with open('writecsv999.csv', 'w', newline='')as f:
        f_csv = csv.DictWriter(f, headers)
        f_csv.writeheader()
        f_csv.writerows(ldata)


if __name__ == '__main__':
# 读取json 文件
    jfile = json.load(open("d:/2/gblab.json", mode="r", encoding="utf8"))
 # 获取到的json文件是个字典,取字典的key获取value值。
    ldata = jfile["RECORDS"]
    # print(ldata)
    # value值形式[{...},{...},{...},{...}...]
    # 把这种方式写入到csv文件中,调用函数 writecsvdata
    writecsvdata(ldata)

    for item in ldata:
        print(item)


There is a problem that has not been solved. After writing into csv with more than 20 digits, it is a garbled problem

Guess you like

Origin blog.csdn.net/wtt234/article/details/113519942