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 + '\n')

file.close()

猜你喜欢

转载自www.cnblogs.com/myblog1993/p/9782735.html