<强> 1。Json读写方法强>
def parseFromFile(自我、帧): ”“” 读取JSON文件覆盖。 ”“” f=开放(帧,“r”) 返回json.load (f) def serializeToFile(自我、帧,注释): ”“” 覆盖编写JSON文件。 ”“” f=开放(帧,“w”) json。转储(注释,f,缩进=4,分隔符=(',',':'),sort_keys=True) f.write(“\ n”) >之前<强> 2。xml文件的工具包XML2Dict 强>
将xml转换成Python本地字典对象,访问子元素和字典常用方法类似,略有不同,使用“。”
注:使用xml2dict库,需要在本地项目添加xml2dict。py, object_dict.py下载链接
加载xml文件
从xml2dict进口xml2dict xml=XML2Dict () r=. parse(“待处理文件名. xml”)xml示例[voc2007格式):
& lt; annotation> & lt; folder> VOC2007 & lt; filename> AL_00001.JPG & lt; size> & lt; width> 800 & lt;/width> & lt; height> 1160 & lt;/height> & lt; depth> 3 & lt;/depth> & lt;/size> & lt; object> & lt; name> l_faster & lt; pose> Unspecified & lt; truncated> 0 & lt;/truncated> & lt; difficult> 0 & lt;/difficult> & lt; bndbox> & lt; xmin> 270 & lt;/xmin> & lt; ymin> 376 & lt;/ymin> & lt; xmax> 352 & lt;/xmax> & lt; ymax> 503 & lt;/ymax> & lt;/bndbox> & lt;/object> & lt; object> & lt; name> l_faster & lt; pose> Unspecified & lt; truncated> 0 & lt;/truncated> & lt; difficult> 0 & lt;/difficult> & lt; bndbox> & lt; xmin> 262 & lt;/xmin> & lt; ymin> 746 & lt;/ymin> & lt; xmax> 355 & lt;/xmax> & lt; ymax> 871 & lt;/ymax> & lt;/bndbox> & lt;/object> & lt; object> & lt; name> r_faster & lt; pose> Unspecified & lt; truncated> 0 & lt;/truncated> & lt; difficult> 0 & lt;/difficult> & lt; bndbox> & lt; xmin> 412 & lt;/xmin> & lt; ymin> 376 & lt;/ymin> & lt; xmax> 494 & lt;/xmax> & lt; ymax> 486 & lt;/ymax> & lt;/bndbox> & lt;/object> & lt; object> & lt; name> r_faster & lt; pose> Unspecified & lt; truncated> 0 & lt;/truncated> & lt; difficult> 0 & lt;/difficult> & lt; bndbox> & lt; xmin> 411 & lt;/xmin> & lt; ymin> 748 & lt;/ymin> & lt; xmax> 493 & lt;/xmax> & lt; ymax> 862 & lt;/ymax> & lt;/bndbox> & lt;/object> & lt;/annotation> >之前<>强分析下这个文件的格式:强>
最外一层被& lt; annotation> & lt;/annotation>包围
往里一层是:& lt; file_name> & lt;/file_name> & lt; size> & lt;/size> & lt; object> & lt;/object>其中对象是列表,包括名字和bndbox,示例访问注释下级元素
# - * -编码:utf - 8 - * 从xml2dict进口xml2dict xml=XML2Dict () r=. parse(注释/AL_00001.xml) 在r.annotation:项 打印项 打印“- - - - - - - - - - - -” 在r.annotation.object:项 打印item.name item.bndbox。xmin item.bndbox。xmax item.bndbox。ymin, item.bndbox.ymax执行结果:
对象 文件夹 大小 价值 文件名 ------------ l_faster 270 352 376 503 l_faster 262 355 746 871 r_faster 412 494 376 486 r_faster 411 493 748 862完整代码(执行xml2json)
# - * -编码:utf - 8 - * 从xml2dict进口xml2dict 进口json 进口一滴 def serializeToFile(帧,注释): ”“” 覆盖编写JSON文件。 ”“” f=开放(帧,“w”) json。转储(注释,f,缩进=4,分隔符=(',',':'),sort_keys=True) f.write(“\ n”) def getAnnos (file_name=" ",前缀="): xml=XML2Dict () 根=. parse (file_name) # dict对象 伊斯兰教纪元=root.annotation image_name=anno.filename 项={“文件名”:前缀+ image_name,“类”:“形象”,“注释”:[]} 在anno.object obj: cls={“l_faster”:“C1”,“r_faster”:“C2”} (obj.name) 盒=obj.bndbox x, y,宽度、高度=int (box.xmin), int (box.ymin), int (box.xmax) int (box.xmin), int (box.ymax) int (box.ymin) 项目(“注释”)+={ “阶级”:cls, “高度”:高度, “宽度”:宽度, “x”: x, “y”: y }) 返回项目 if __name__==癬_main__”: 注释=[] anno_name=' ar_001 - 550. - json ' 文件=glob.glob(注释/AR_ * . xml) 文件=排序(文件) #打印files.sort () 文件名的文件: 项=getAnnos(文件名前缀=' TFS JPEGImages/') 打印项 打印”- - - - - - - - - - - - - - - - - -” 注释+=[项目]#“xml/AL_00001.xml” serializeToFile (anno_name,注释)Python XML转Json之XML2Dict的使用方法