Python中的xml與dict的轉(zhuǎn)換方法詳解
接口文檔拿到的是XML,在線轉(zhuǎn)化為json格式(目的是拿到xml數(shù)據(jù)的模板),存放到j(luò)son文件中,根據(jù)接口名去提取。
- xml 是指可擴(kuò)展標(biāo)記語言,一種標(biāo)記語言類似html,作用是傳輸數(shù)據(jù),而且不是顯示數(shù)據(jù)。可以自定義標(biāo)簽。
- Python 中的xml和dict 互相轉(zhuǎn)化。使用的模塊是xmltodict。
import re import xmltodict xml="""<notes> <to>demo</to> <from>哈哈</from> <header>呵呵</header> <body>"尼古拉斯趙四"</body> </notes>""" dict = {"goods":{"fruits":{"name":"melon","coloer":"red","nut":"walnut"}}} class XmlToDict(object): def get_dict(self,xml): """xml to dict""" return xmltodict.parse(xml_input=xml,encoding="utf-8") def get_xml_content(self,orderdict): for i in orderdict: print(orderdict[str(i)]) def get_content(self,xml): first_title = re.match(r"<.*>", xml).group()[1:-1] orderdict = self.get_dict(xml) orderdict=orderdict[first_title] self.get_xml_content(orderdict) def dicttoxml(self,dict): """dict to xml""" return xmltodict.unparse(dict,encoding="utf-8") if __name__ == '__main__': XmlToDict().get_content(xml) ret=XmlToDict().dicttoxml(dict) print(ret)
- python 中還有一個(gè)模塊dicttoxml ,將字典轉(zhuǎn)成xml
mport dicttoxml dict= {"goods":{"fruits":{"name":"melon","coloer":"red","nut":"walnut"}}} ret_xml = dicttoxml.dicttoxml(dict,custom_root="Request",root=True).decode("utf-8") # 默認(rèn)是byte 類型,轉(zhuǎn)成str。 print(type(ret_xml))
利用循環(huán)字典轉(zhuǎn)成xml
dict = { "fruit": "apple", "goods": "hamburger" } def dicttoxml(iKwargs): xml = [] for k in sorted(iKwargs.keys()): v =iKwargs.get(k) xml.append("<{key}>{value}</{key}>".format(key=k,value=v)) return "<xml>{}</xml>".format("".join(xml)) ret=dicttoxml(dict) print(ret)
上面就是xml和dict轉(zhuǎn)化,如果需要轉(zhuǎn)化json,內(nèi)置的json模塊就可以完成,但是在自動(dòng)化測試框架中這樣使用比較麻煩,而且復(fù)用性不好,封裝好如下
import xmltodict """ xml和dict轉(zhuǎn)換 """ def dict_xml(dictdata): """ dict轉(zhuǎn)xml dictstr: dict字符串 return: xml字符串 """ xmlstr=xmltodict.unparse(dictdata, pretty=True) return xmlstr def xml_dict(xmldata,moudle): """ xml轉(zhuǎn)dict xmlstr: xml字符串 moudle:根節(jié)點(diǎn) return: dict字符串 """ data=xmltodict.parse(xmldata,process_namespaces = True) dictdata=dict(data) _dictdata=dict(dictdata[moudle]) dictdata[moudle]=_dictdata return dictdata
到此這篇關(guān)于Python中的xml與dict的轉(zhuǎn)換方法詳解的文章就介紹到這了,更多相關(guān)Python中的xml與dict轉(zhuǎn)換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)高分辨率圖像導(dǎo)航的代碼
高分辨率圖像導(dǎo)航是一種技術(shù),它允許用戶在大型圖像中進(jìn)行導(dǎo)航和瀏覽,而無需加載整個(gè)圖像到內(nèi)存中,在本文中,我們將使用30行Python代碼實(shí)現(xiàn)這一功能,我們將使用Python的圖像處理庫和計(jì)算機(jī)視覺庫來加載圖像數(shù)據(jù)并生成高分辨率圖像導(dǎo)航2024-03-03Python判斷兩個(gè)list是否是父子集關(guān)系的實(shí)例
今天小編就為大家分享一篇Python判斷兩個(gè)list是否是父子集關(guān)系的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05使用 Python 玩轉(zhuǎn) GitHub 的貢獻(xiàn)板(推薦)
這篇文章主要介紹了使用 Python 玩轉(zhuǎn) GitHub 的貢獻(xiàn)板的相關(guān)知識(shí),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04Scrapy-Redis之RedisSpider與RedisCrawlSpider詳解
這篇文章主要介紹了Scrapy-Redis之RedisSpider與RedisCrawlSpider詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Python使用PySimpleGUI和Pygame編寫一個(gè)MP3播放器
這篇文章主要為大家詳細(xì)介紹了Python如何使用PySimpleGUI和Pygame編寫一個(gè)簡單的MP3播放器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以學(xué)習(xí)一下2023-11-11Python調(diào)用ChatGPT的API實(shí)現(xiàn)文章生成
最近ChatGPT大火,在3.5版本后開放了接口API,所以很多人開始進(jìn)行實(shí)操,這里我就用python來為大家實(shí)現(xiàn)一下,如何調(diào)用API并提問返回文章的說明2023-03-03