想实现提取json文件中的标记为fake的视频文件名,开始时间,结束时间,3者以一个dict的形式追加到list中,但是单看每个dict内容对,但是最后的list中显示的是重复多次的最后一个dict的值。
解决方法:data_row_info.append() 修改为data_row_info.append(d.copy())
附上代码:
# -*- coding: utf-8 -*-
import json
import csv
data_row_info = list()
d = dict()
data_row_info_headers = ["文件名","开始时间","结束时间"]
with open('test.json', 'r', newline='', encoding='utf-8') as f:
user_dic = json.load(f)
filename = user_dic["path"]
slices_list = user_dic["outputs"]["object"]
for i in slices_list:
name = i["name"]
if "fake" in name:
keyframes_list = i["keyframes"]
if len(keyframes_list)==2:
begin_time = keyframes_list[0]["time"]
end_time = keyframes_list[1]["time"]
d["文件名"] = filename
d["开始时间"] = begin_time
d["结束时间"] = end_time
#print(d)
data_row_info.append(d.copy())
print(data_row_info)
with open("all_videos.csv", 'w', newline='',encoding='utf-8') as f:
f_csv = csv.DictWriter(f, data_row_info_headers)
f_csv.writeheader()
f_csv.writerows(data_row_info)
阅读(812) | 评论(0) | 转发(0) |