python parsing json to csv file information in

json variety of formats, the code focused watch function portion

json Import, CSV, OS 
Import PANDAS AS PD 
josns_root = 'jsons' 
csvs_root = 'CSVS' 
list_josn = the os.listdir (josns_root) 
for BB in list_josn: 
    path_json_ = BB # edit json path 
    path_json = os.path.join (josns_root , path_json_) 
    path_csv_ = path_json_.split () [0] + '.' 'CSV.' 
    path_csv = the os.path.join (csvs_root, path_csv_) 
    IF os.path.exists (path_csv): 
        Print ( 'existing path, resolve the conflict ', path_csv) 
        Exit () 
    Open (path_csv,' W '). Close () 
    list_ = [200,50,51] 
    F = Open (path_json, encoding =' UTF-. 8 ') loaded json file # 
    = the json.load Setting (F) 
    Data Setting = [ 'Data']
    data1 = data['imgs']]
    print(type(data1))
    headers = ['fov_id', 'cells_fit', 'cells_P', 'cells_N']
    with open(path_csv, 'a')as csvfile:
         writer = csv.writer(csvfile)
         writer.writerow(headers) # 写每列标题
         csvfile.close()
    cnt = 1
    for i in data1:
        print('当前FOV编号:',cnt)
        cnt = cnt + 1
        i_dict = dict(i)
        FOV_id = i_dict['id']
        cells = i_dict['cells']
        type_la = []
        total_la = []
        for m in cells:
            m_dict = dict(m)
            type_ = m_dict['type']
            total = m_dict['total']
            #print(type_, total)
            type_la.append(type_)
            total_la.append(total)
        out_ = dict(zip(type_la,total_la))
        str1 = [None,None,None]
        cnt_ = 0
        cnt__ = 0
        for lala in list_:
            if len(list(out_.keys())) == 0:
                continue
            if lala in list(out_.keys()):
                str1[cnt_] = list(out_.values())[cnt__]
                cnt__ = cnt__ + 1
            cnt_ = cnt_ + 1
        rows = [str(FOV_id), str1[0], str1[2], str1[1]]
        with open(path_csv, 'a')as csvfile:
            writers = csv.writer(csvfile, delimiter=',')
            writers.writerow(rows) # 按行写入csv
            csvfile.close()
    print('==============task OK!================')

  

Guess you like

Origin www.cnblogs.com/niulang/p/12096239.html