博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 将字典写入csv
阅读量:4114 次
发布时间:2019-05-25

本文共 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/

你可能感兴趣的文章
菜单树
查看>>
MySQL-分布式架构-MyCAT
查看>>
设计模式六大原则(6):开闭原则
查看>>
阿里面试总结--JAVA
查看>>
Servlet的生命周期
查看>>
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>
【设计模式】—-(2)工厂方法模式(创建型)
查看>>
有return的情况下try catch finally的执行顺序(最有说服力的总结)
查看>>
String s1 = new String("abc"); String s2 = ("abc");
查看>>
JAVA数据类型
查看>>
Xshell 4 入门
查看>>
SoapUI-入门
查看>>
Oracle -常用命令
查看>>
JAVA技术简称
查看>>
ORACLE模糊查询优化浅谈
查看>>
2016——个人年度总结
查看>>
2017——新的开始,加油!
查看>>
【Python】学习笔记——-6.2、使用第三方模块
查看>>
【Python】学习笔记——-7.0、面向对象编程
查看>>