python xml模块
生活随笔
收集整理的這篇文章主要介紹了
python xml模块
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
'''
XML格式
首先,來(lái)看一下XML所包含的元素類(lèi)型
1. 標(biāo)簽 <tag>
2. 屬性 <tag name="attribute">
3. 數(shù)據(jù) <data>1<data>
'''
import xml.etree.cElementTree as et
file_path = "xml_text.xml"
'''
創(chuàng)建xml文件
'''
def create_xml():
# 創(chuàng)建根節(jié)點(diǎn)
root = et.Element('data')
# 向父節(jié)點(diǎn)添加子節(jié)點(diǎn)
country1 = et.SubElement(root,'country')
# 給節(jié)點(diǎn)添加屬性
country1.attrib = {'name':'Austria','direction': 'E'}
son = et.SubElement(country1, 'son')
son.attrib={"name":"test"}
son.text="test"
# 創(chuàng)建elementtree對(duì)象
tree = et.ElementTree(root)
# 寫(xiě)入文件
tree.write(file_path)
'''
讀取xml文件或字符
'''
def read_xml():
str_xml = '''<data><country direction="E" name="Austria"><son name="test">test</son></country></data>'''
# 將xml字符串轉(zhuǎn)為element
root = et.fromstring(str_xml)
# 讀取xml文件轉(zhuǎn)為ElementTree對(duì)象
# tree = et.parse(file_path)
# root = tree.getroot()
for i in root:
print( i.tag)
print(i.attrib)
print(i.text)
# 查找第一個(gè)標(biāo)簽為son的元素
print(i.find("son"))
# 查找所有標(biāo)簽為son的元素
print(i.findall('son'))
# 遍歷所有標(biāo)簽為son的元素
for j in i.iter('son'):
print(j)
'''
修改xml
'''
def modify_xml():
tree = et.parse(file_path)
root = tree.getroot()
country = root.find("country")
son = country.find("son")
# 設(shè)置屬性
son.set('name','newvalue')
# 設(shè)置text值
son.text='newtext'
tree.write(file_path)
if __name__ == '__main__':
# create_xml()
# read_xml()
modify_xml()
XML格式
首先,來(lái)看一下XML所包含的元素類(lèi)型
1. 標(biāo)簽 <tag>
2. 屬性 <tag name="attribute">
3. 數(shù)據(jù) <data>1<data>
'''
import xml.etree.cElementTree as et
file_path = "xml_text.xml"
'''
創(chuàng)建xml文件
'''
def create_xml():
# 創(chuàng)建根節(jié)點(diǎn)
root = et.Element('data')
# 向父節(jié)點(diǎn)添加子節(jié)點(diǎn)
country1 = et.SubElement(root,'country')
# 給節(jié)點(diǎn)添加屬性
country1.attrib = {'name':'Austria','direction': 'E'}
son = et.SubElement(country1, 'son')
son.attrib={"name":"test"}
son.text="test"
# 創(chuàng)建elementtree對(duì)象
tree = et.ElementTree(root)
# 寫(xiě)入文件
tree.write(file_path)
'''
讀取xml文件或字符
'''
def read_xml():
str_xml = '''<data><country direction="E" name="Austria"><son name="test">test</son></country></data>'''
# 將xml字符串轉(zhuǎn)為element
root = et.fromstring(str_xml)
# 讀取xml文件轉(zhuǎn)為ElementTree對(duì)象
# tree = et.parse(file_path)
# root = tree.getroot()
for i in root:
print( i.tag)
print(i.attrib)
print(i.text)
# 查找第一個(gè)標(biāo)簽為son的元素
print(i.find("son"))
# 查找所有標(biāo)簽為son的元素
print(i.findall('son'))
# 遍歷所有標(biāo)簽為son的元素
for j in i.iter('son'):
print(j)
'''
修改xml
'''
def modify_xml():
tree = et.parse(file_path)
root = tree.getroot()
country = root.find("country")
son = country.find("son")
# 設(shè)置屬性
son.set('name','newvalue')
# 設(shè)置text值
son.text='newtext'
tree.write(file_path)
if __name__ == '__main__':
# create_xml()
# read_xml()
modify_xml()
轉(zhuǎn)載于:https://www.cnblogs.com/lides/p/11116713.html
總結(jié)
以上是生活随笔為你收集整理的python xml模块的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: centos 7.3 开放端口并对外开放
- 下一篇: websocket python爬虫_p