XML(可擴展標記語言)是一種廣泛使用的半結構化數據格式。在處理XML數據時,Python提供了各種第三方庫。其中,xmltodict是一個非常流行的解析器,它可以將XML數據轉換成Python 字典,以便更方便地處理。
一、安裝和使用
xmltodict可以通過pip安裝:
pip install xmltodict
安裝後,可以import xmltodict導入。
XML文件可以作為文件讀取,使用with open()方法即可。
import xmltodict
with open('example.xml') as f:
data = xmltodict.parse(f.read())
該解析器可以處理XML文件,甚至是使用HTTP GET請求獲得的XML響應。使用requests庫做一個簡單的例子:
import requests
import xmltodict
url = 'http://www.example.com/example.xml'
r = requests.get(url)
data = xmltodict.parse(r.content)
二、XML轉換為Python字典
xmltodict.parse()方法可以將XML文件轉換成Python字典。以下是一個簡單的XML文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
使用xmltodict.parse()方法將其轉換成Python字典:
import xmltodict
xml_str = '''
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
'''
data = xmltodict.parse(xml_str)
print(data)
輸出結果:
{'catalog': {'book': {'@id': 'bk101', 'author': 'Gambardella, Matthew', 'title': "XML Developer's Guide", 'genre': 'Computer', 'price': '44.95', 'publish_date': '2000-10-01', 'description': 'An in-depth look at creating applications \n with XML.'}}}
可以看到,XML文件的每個元素都轉換成了Python字典的鍵和值,每個元素內的屬性也轉換成了Python字典的鍵和值。如果XML文件中有子元素,則會將其轉換為Python字典中的字典。注意,每個元素的值都是作為字符串存儲的,需要進行類型轉換。
三、Python字典轉換為XML
xmltodict.unparse()方法可以將Python字典轉換成XML文件。以下是一個簡單的Python字典示例:
import xmltodict
dictionary = {'catalog': {'book': {'@id': 'bk101', 'author': 'Gambardella, Matthew', 'title': "XML Developer's Guide", 'genre': 'Computer', 'price': 44.95, 'publish_date': '2000-10-01', 'description': 'An in-depth look at creating applications with XML.'}}}
使用xmltodict.unparse()方法將其轉換成XML格式:
import xmltodict
dictionary = {'catalog': {'book': {'@id': 'bk101', 'author': 'Gambardella, Matthew', 'title': "XML Developer's Guide", 'genre': 'Computer', 'price': 44.95, 'publish_date': '2000-10-01', 'description': 'An in-depth look at creating applications with XML.'}}}
xml_str = xmltodict.unparse(dictionary)
print(xml_str)
輸出結果:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
可以看到,將Python字典轉換成XML格式的過程與將XML文件轉換成Python字典的過程正好相反。 注意,數值類型的值不需要額外的引號。在生成的XML文件中,每個元素的值都被排版成了一行。
四、XML文件中的特殊字符和轉義字符
XML文件中有一些特殊字符和轉義字符,如<、>等,將其放在XML文件中時需要對其進行轉義,否則會導致解析器無法正確解析。 xmltodict.parse()方法以及xmltodict.unparse()方法可以正確的處理這些特殊字符和轉義字符。以下是一個包含特殊字符和轉義字符的XML文件示例:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella & Drukman</author>
<title><![CDATA[XML Developer's Guide]]></title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
將其解析成Python字典,並將結果轉換成XML格式:
import xmltodict
xml_str = '''
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella & Drukman</author>
<title><![CDATA[XML Developer's Guide]]></title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
</catalog>
'''
data = xmltodict.parse(xml_str)
xml_str = xmltodict.unparse(data)
print(xml_str)
輸出結果:
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<book id="bk101">
<author>Gambardella & Drukman</author>
<title><![CDATA[XML Developer's Guide]]></title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
</catalog>
可以看到,解析器可以正確地處理XML文件中的特殊字符和轉義字符。將Python字典轉換成XML格式時,特殊字符和轉義字符也會被正確地轉換。
五、結語
xmltodict是一個非常強大的解析器,它可以將XML文件轉換成Python字典,並將Python字典轉換成XML文件。使用該解析器可以非常便捷地處理XML數據。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/190684.html