首页 > 编程 > Python > 正文

Python XML转Json之XML2Dict的使用方法

2020-02-16 00:09:08
字体:
来源:转载
供稿:网友

1. Json读写方法

def parseFromFile(self, fname):  """  Overwritten to read JSON files.  """  f = open(fname, "r")  return json.load(f)def serializeToFile(self, fname, annotations):  """  Overwritten to write JSON files.  """  f = open(fname, "w")  json.dump(annotations, f, indent=4, separators=(',', ': '), sort_keys=True)  f.write("/n")

2. xml文件的工具包XML2Dict

将xml转换成Python本地字典对象, 访问子元素和字典常用方法类似,略有不同, 使用 “.”

注: 使用xml2dict库,需要在本地项目添加 xml2dict.py, object_dict.py,下载链接

加载xml文件

from xml2dict import XML2Dictxml = XML2Dict()r = xml.parse("待处理文件名.xml") 

xml示例[voc2007格式]:

<annotation>  <folder>VOC2007</folder>  <filename>AL_00001.JPG</filename>  <size>    <width>800</width>    <height>1160</height>    <depth>3</depth>  </size>  <object>    <name>l_faster</name>    <pose>Unspecified</pose>    <truncated>0</truncated>    <difficult>0</difficult>    <bndbox>      <xmin>270</xmin>      <ymin>376</ymin>      <xmax>352</xmax>      <ymax>503</ymax>    </bndbox>  </object>  <object>    <name>l_faster</name>    <pose>Unspecified</pose>    <truncated>0</truncated>    <difficult>0</difficult>    <bndbox>      <xmin>262</xmin>      <ymin>746</ymin>      <xmax>355</xmax>      <ymax>871</ymax>    </bndbox>  </object>  <object>    <name>r_faster</name>    <pose>Unspecified</pose>    <truncated>0</truncated>    <difficult>0</difficult>    <bndbox>      <xmin>412</xmin>      <ymin>376</ymin>      <xmax>494</xmax>      <ymax>486</ymax>    </bndbox>  </object>  <object>    <name>r_faster</name>    <pose>Unspecified</pose>    <truncated>0</truncated>    <difficult>0</difficult>    <bndbox>      <xmin>411</xmin>      <ymin>748</ymin>      <xmax>493</xmax>      <ymax>862</ymax>    </bndbox>  </object></annotation>

分析下这个文件的格式:

最外一层被<annotation></annotation>包围

往里一层是:<file_name></file_name>,<size></size>,<object></object>,其中object是列表,包括name和bndbox,示例访问annotation下级元素

# -*- coding: utf-8 -*-from xml2dict import XML2Dictxml = XML2Dict()r = xml.parse('Annotations/AL_00001.xml')for item in r.annotation:  print itemprint '------------'for item in r.annotation.object:  print item.name, item.bndbox.xmin, item.bndbox.xmax, item.bndbox.ymin, item.bndbox.ymax            
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表