欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python XML轉(zhuǎn)Json之XML2Dict的使用方法

 更新時(shí)間:2019年01月15日 09:54:28   作者:_icrazy_  
今天小編就為大家分享一篇Python XML轉(zhuǎn)Json之XML2Dict的使用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

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轉(zhuǎn)換成Python本地字典對(duì)象, 訪問(wèn)子元素和字典常用方法類似,略有不同, 使用 “.”

注: 使用xml2dict庫(kù),需要在本地項(xiàng)目添加 xml2dict.py, object_dict.py,下載鏈接

加載xml文件

from xml2dict import XML2Dict
xml = 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>

分析下這個(gè)文件的格式:

最外一層被<annotation></annotation>包圍

往里一層是:<file_name></file_name>,<size></size>,<object></object>,其中object是列表,包括name和bndbox,示例訪問(wèn)annotation下級(jí)元素

# -*- coding: utf-8 -*-
from xml2dict import XML2Dict
xml = XML2Dict()
r = xml.parse('Annotations/AL_00001.xml')
for item in r.annotation:
  print item
print '------------'
for item in r.annotation.object:
  print item.name, item.bndbox.xmin, item.bndbox.xmax, item.bndbox.ymin, item.bndbox.ymax

執(zhí)行結(jié)果:

object
folder
size
value
filename
------------
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]

# -*- coding: utf-8 -*-
from xml2dict import XML2Dict
import json
import glob


def serializeToFile(fname, annotations):
  """
  Overwritten to write JSON files.
  """
  f = open(fname, "w")
  json.dump(annotations, f, indent=4, separators=(',', ': '), sort_keys=True)
  f.write("\n")

def getAnnos(file_name="", prefix=''):
  xml = XML2Dict()
  root = xml.parse(file_name)
  # get a dict object
  anno = root.annotation
  image_name = anno.filename
  item = {'filename': prefix + image_name, 'class': 'image', 'annotations': []}

  for obj in anno.object:

    cls = {'l_faster': 'C1', 'r_faster': 'C2'}[obj.name]
    box = obj.bndbox
    x, y, width, height = int(box.xmin), int(box.ymin), int(box.xmax) - int(box.xmin), int(box.ymax) - int(box.ymin)
    item['annotations'] += [{
        "class": cls,
        "height": height,
        "width": width,
        "x": x,
        "y": y
      }]
  return item

if __name__ == '__main__':
  annotations = []
  anno_name = 'AR_001-550.json'
  files = glob.glob('Annotations/AR_*.xml')
  files = sorted(files)
  # print files.sort()
  for filename in files:
    item = getAnnos(filename, prefix='TFS/JPEGImages/')
    print item
    print '-----------------'
    annotations += [item] #"xmls/AL_00001.xml"
  serializeToFile(anno_name, annotations)


以上這篇Python XML轉(zhuǎn)Json之XML2Dict的使用方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于python實(shí)現(xiàn)從尾到頭打印鏈表

    基于python實(shí)現(xiàn)從尾到頭打印鏈表

    這篇文章主要介紹了基于python實(shí)現(xiàn)從尾到頭打印鏈表,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-11-11
  • Python中l(wèi)ist初始化方法示例

    Python中l(wèi)ist初始化方法示例

    這篇文章主要介紹了Python中l(wèi)ist初始化方法,分析了list初始化常用的方法與相關(guān)使用注意事項(xiàng),需要的朋友可以參考下
    2016-09-09
  • Python序列之list和tuple常用方法以及注意事項(xiàng)

    Python序列之list和tuple常用方法以及注意事項(xiàng)

    這篇文章主要介紹了Python序列之list和tuple常用方法以及注意事項(xiàng),sequence(序列)是一組有順序的對(duì)象的集合,序列可以包含一個(gè)或多個(gè)元素,也可以沒有任何元素,序列有兩種:list (表) 和 tuple(元組),需要的朋友可以參考下
    2015-01-01
  • 關(guān)于Pandas?count()與values_count()的用法及區(qū)別

    關(guān)于Pandas?count()與values_count()的用法及區(qū)別

    這篇文章主要介紹了關(guān)于Pandas?count()與values_count()的用法及區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python 同時(shí)運(yùn)行多個(gè)程序的實(shí)例

    python 同時(shí)運(yùn)行多個(gè)程序的實(shí)例

    今天小編就為大家分享一篇python 同時(shí)運(yùn)行多個(gè)程序的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • 重溫Python基礎(chǔ)之列表操作

    重溫Python基礎(chǔ)之列表操作

    這篇文章主要帶大家來(lái)復(fù)習(xí)一下Python基礎(chǔ)中的列表操作,不知道各位還記得多少呢?文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下
    2022-11-11
  • python+selenium 點(diǎn)擊單選框-radio的實(shí)現(xiàn)方法

    python+selenium 點(diǎn)擊單選框-radio的實(shí)現(xiàn)方法

    今天小編就為大家分享一篇python+selenium 點(diǎn)擊單選框-radio的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-09-09
  • Python中的迭代器與生成器使用及說(shuō)明

    Python中的迭代器與生成器使用及說(shuō)明

    這篇文章主要介紹了Python中的迭代器與生成器使用及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • 在Tensorflow中實(shí)現(xiàn)梯度下降法更新參數(shù)值

    在Tensorflow中實(shí)現(xiàn)梯度下降法更新參數(shù)值

    今天小編就為大家分享一篇在Tensorflow中實(shí)現(xiàn)梯度下降法更新參數(shù)值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-01-01
  • Python Tornado實(shí)現(xiàn)WEB服務(wù)器Socket服務(wù)器共存并實(shí)現(xiàn)交互的方法

    Python Tornado實(shí)現(xiàn)WEB服務(wù)器Socket服務(wù)器共存并實(shí)現(xiàn)交互的方法

    這篇文章主要介紹了Python Tornado實(shí)現(xiàn)WEB服務(wù)器Socket服務(wù)器共存并實(shí)現(xiàn)交互的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05

最新評(píng)論