本文共 1109 字,大约阅读时间需要 3 分钟。
import osimport jsonimport csvimport os.pathfrom pathlib import Path# os.path.isfile(fname)root='/faster_rcnn'save='sum.csv'h=['name','AP','AP50','AP75','bAP','bAP50','bAP75','nAP','nAP50','nAP75']with open(save,'w',newline='') as f: writer = csv.DictWriter(f,fieldnames=h) writer.writeheader() for d in os.listdir(root): tmp_root = root + '/' + d for dd in os.listdir(tmp_root): if dd == 'inference': cur = tmp_root + '/' + dd + '/res_final.json' if Path(cur).exists(): with open(cur) as fp: read = json.load(fp) dic = read['bbox'] dic['name'] = d print(d) writer.writerow(dic)
使用csv包
主要语句:h=['name','AP','AP50','AP75','bAP','bAP50','bAP75','nAP','nAP50','nAP75']with open(save,'w',newline='') as f: writer = csv.DictWriter(f,fieldnames=h) #构造一个write writer.writeheader() #写入表头 writer.writerow(dic) #将数据写入csv,会根据字典的键值对自动匹配表头
如果表头是字典形式,写入顺序和字典定义顺序不同,如果表头h定义的时候是lsit形式,在csv文件中写入的表头的顺序和定义的h顺序一样
转载地址:http://ekgsi.baihongyu.com/