码迷,mamicode.com
首页 > 编程语言 > 详细

Python读写XML后保持节点属性顺序不变

时间:2018-12-12 22:43:36      阅读:461      评论:0      收藏:0      [点我收藏+]

标签:ini   attr   init   block   import   2.7   code   when   读取   

Python读写XML后保持节点属性顺序不变

试过xml.etree.ElementTree和xml.dom.minidom两个python的库,发现读取后输出都会改变xml里面节点属性的顺序. 虽然这个顺序其实没什么意义但是有些时候时候会比较纠结,找了好多资料最后在stackoverflow中找到一些有用的资料.最后亲测可用.

原资料地址

python环境:2.7

库:import xml.dom.minidom

  • 修改minidom源码,引入相应的库
from collections import OrderedDict
  • 根据下面代码 注释掉相应的源码 新增 self._attrs = OrderedDict()
__init__(...)
    self._attrs = OrderedDict()
    #self._attrs = {}
writexml(...)
    #a_names.sort()

Python读写XML后保持节点属性顺序不变

标签:ini   attr   init   block   import   2.7   code   when   读取   

原文地址:https://www.cnblogs.com/crazyzero/p/10111364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!