python处理xml实例
Posted lllittletree
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python处理xml实例相关的知识,希望对你有一定的参考价值。
!/usr/bin/env python
-- coding: utf-8 --
"""
Author = zyh
FileName = read_xml_1.py
Time = 18-9-26 下午5:19
"""
from xml.etree import ElementTree
import os
path = (‘/home/zyh/Desktop/xml17‘)
xml_file_list = os.listdir(path)
print(xml_file_list)
read_list = []
with open(‘./riskCar_pic.txt‘, ‘r‘, encoding=‘utf-8‘) as file:
for line in file:
read_list = line.strip()
for xml in xml_file_list:
full_path = os.path.join(path, xml)
print(full_path)
root = ElementTree.parse(full_path)
# print(root)
name_node = root.getiterator(‘name‘)
# print(len(name_node))
for node in name_node:
print(node.text)
if node.text == ‘riskCar‘:
if xml not in read_list:
with open(‘./riskCar_pic.txt‘, ‘a‘, encoding=‘utf-8‘) as f:
f.write(xml + ‘
‘)
file.close()
以上是关于python处理xml实例的主要内容,如果未能解决你的问题,请参考以下文章