xml 模块介绍
和json一样,适用于不同语言及程序的数据交换的协议。但是json用起来更简单,并有代替xml的趋势。 现在多数金融数据提供方(e.g. bloombegy)还在用xml的方式。
在python中,生成和解析 XML 文件 用 导入 xml.etree.ElementTree 模块
xml文件的格式
xml的格式如下,就是通过<>节点来区别数据结构的:
xml的格式
常用操作
读取xml
import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml") root = tree.getroot() print(root.tag) # 返回 data # 历遍xml文档 for child in root: print(child.tag, child.attrib) #返回country 层 for i in child: print(i.tag, i.text) # 只历遍year节点, 选择数据历遍 for node in root.iter("year"): print(node.tag,node.text)
修改删除xml文件内容
import xml.etree.ElementTree as ET tree = ET.parse("xmltest.xml") root = tree.getroot() #修改数据 for node in root.iter("year"): new_year = int(node.text) + 1 # 每年都增加1 node.text = str(new_year) # 赋值 node.set("updated","yes") # 同时,修改attribution 属性 tree.write("xmltest.xml") # 删除某个node for country in root.findall("country"): rank = int(country.find('rank').text) if rank > 50: root.remove(country) tree.write("output.xml")
创建新xml文件
import xml.etree.ElementTree as ET new_xml = ET.Element("namelist") # 创建根节点 personinfo = ET.SubElement(new_xml,"personinfo",attrib={"enrolled":"yea"}) name = ET.SubElement(personinfo,"name") name.text = "alex li" age = ET.SubElement(personinfo,"age",attrib={"checked":"no"}) age.text = "56" sex = ET.SubElement(personinfo,"sex") sex.text = "F" personinfo2 = ET.SubElement(new_xml,"personinfo", attrib={"enrolled": "yes"}) name = ET.SubElement(personinfo2, "name") name.text = "oldboy ran" age = ET.SubElement(personinfo2, "age") age.text = "22" et = ET.ElementTree(new_xml) # 生成文档对象 et.write("new_xml_test.xml", encoding="utf-8", xml_declaration=True) ET.dump(new_xml) # 打印生成的格式