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()
