将json文件批量转为csv格式数据并整合成一个csv文档

本文要处理的数据集格式如下:

-00001

        -000_00

               -21.json

               -22.json

               -23.json

               -……

         -000_01

         -015_00

         -015_01

          -……

def csv_json():
    path = osp.join('E:/downloads/1', "*", "*")
    path2 = osp.join(path, "*")
    samples1 = sorted(glob.glob(path2))
    for sequence in tqdm(samples1):#
        p_id, info = sequence.split(osp.sep)[-2:]
        angle, seq_num = info.split("_")
        keypoints = []
        frame_nums = []
        st = []
        csv_fp = open('E:/downloads/1/train.csv', 'a', encoding='utf-8', newline='')#训练集
        csv_fp2 = open('E:/downloads/1/train1.csv', 'a', encoding='utf-8', newline='')#训练集标签
        csv_fp1 = open('E:/downloads/1/test.csv', 'a', encoding='utf-8', newline='')#测试集
        csv_fp3 = open('E:/downloads/1/test1.csv', 'a', encoding='utf-8', newline='')#测试集标签
        for file in sorted(glob.glob(osp.join(sequence, "*.json"))):  # 遍历每个json
            with open(file) as f:
                d = json.load(f)
            if len(d["people"]) == 0:
                continue
            pdata = d["people"][0]["pose_keypoints_2d"]#读取json文件里想要的数据传进pdata
            frame_num = osp.basename(file).split("_")
            frame_num = int(frame_num[0]) if "-" not in frame_num[0] else int(frame_num[-1].split("-")[-1][:-5])
            frame_nums.append(frame_num)
            st.append([str("./" + p_id + "-nm-" + seq_num + "-" + angle + "/" + str(frame_num) + ".jpg")])#制定标签格式
            keypoints.append(pdata)
        writer1 = csv.writer(csv_fp)#训练集
        writer2 = csv.writer(csv_fp2)#训练集标签
        writer3 = csv.writer(csv_fp1)#测试集
        writer4 = csv.writer(csv_fp3)#测试集标签
        if (int(p_id)%2)==0 or int(p_id)==10307:
            writer4.writerows(st)#测试集标签
            writer3.writerows(keypoints)
        else:
            writer2.writerows(st)
            writer1.writerows(keypoints)
        keypoints.clear()
        frame_nums.clear()
        st.clear()
        csv_fp.close()
        csv_fp2.close()
        csv_fp1.close()
        csv_fp3.close()

if __name__ == "__main__":
    csv_json()

你们的每个赞都能让我开心好几天✿✿ヽ(°▽°)ノ✿

你可能感兴趣的:(json)