python,比较两个xml - 公司荣誉 - 长春市隆兴伟业物流有限公司
现在的位置: 主页 > 公司荣誉 > 文章正文
python,比较两个xml
作者:长春市隆兴伟业物流有限公司 来源:www.lxwywl.com 发布时间:2017-09-12 16:25:26
python,比较两个xml



# -*- coding: UTF-8 -*-
#作用:查询表一表二的不同内容,表1相对于表2 ,表2相对于表1
#data1生成名字的list2
from __future__ import division
import csv
import re
import os
import math
import sys
from bs4 import BeautifulSoup
from tkinter.filedialog import askopenfilename
import pandas as pd
#定义进度条
def progressbar(cur, total):
percent = '{:.2%}'.format(cur / total)
sys.stdout.write('\r')
sys.stdout.write("[%-50s] %s" % ('=' * int(math.floor(cur * 50 / total)),
percent))
sys.stdout.flush()

print('第一份xml文件')
filename1 = askopenfilename(filetypes=( ("Text file", "*.xml*"),("HTML files", "*.html;*.htm")))
print('第二份xml文件')
filename2 = askopenfilename(filetypes=( ("Text file", "*.xml*"),("HTML files", "*.html;*.htm")))
print('处理中,大约20来秒,处理结果在D盘diff文件夹内')
print('处理进度')
progressbar(1, 100)

#创建文件夹目录
if(os.path.exists('D:\diff')):
pass
else:
os.mkdir("D:\diff")
#xml2csv 把xml文件转换成csv
header = ['Name', 'InitialValue', 'BachmannAddress', 'CalibrationExpression', 'IOInitialValue']

def parse_channel(tag):
name = tag.find("Name")
initial_value = tag.find("InitialValue")
bachmann_address = tag.find("BachmannAddress")
calibration_expression = tag.find("CalibrationExpression")
io_initial_value = tag.find("IOInitialValue")
return {
"Name": name.text if name else "",
"InitialValue": initial_value.text if initial_value else "",
"BachmannAddress": bachmann_address.text if bachmann_address else "",
"CalibrationExpression": calibration_expression.text if calibration_expression else "",
"IOInitialValue": io_initial_value.text if io_initial_value else "",
}

def dump_file(data_list, file_name):
df = pd.DataFrame(data_list)
# 去除空行
df.dropna()
# 去重
df.drop_duplicates()
#df.to_csv(file_name, index=False, header=header)
df.to_csv(file_name, index=False, columns=header)

def xml2csv(input_file, output_file):
raw_text = open(input_file,'rb').read()
soup = BeautifulSoup(raw_text, "xml")
data_list = [parse_channel(i) for i in soup.find("Channels").find_all("Channel")]
dump_file(data_list=data_list, file_name=output_file)

# xml2csv(input_file="raw.xml", output_file="post.csv")

def zhaozuzhi(a,b): #a是多的那一个,b是少的
t=[]
for i in range(len(a)):
for j in b:
if a[i][0]==j:
t.append([a[i]])
return t
#从三个列表中依次读取第一个存入一行存入一个list 形成name,data1,data2的模样
def walk_list(a,b,c):
list=[]
for i in range(len(a)):
list.append([a[i],b[i],c[i]])
return list
#将xml转换成csv
xml2csv(input_file=filename1, output_file="D:/data1.csv")
xml2csv(input_file=filename2, output_file="D:/data2.csv")
#读取第一个csv,生成以name名字为元素的list2
progressbar(30, 100)
with open('D:\data1.csv','r') as f:
readers1=csv.reader(f)
list1=[row for row in readers1]
list2=[]
for i in range(len(list1)):
list2.append(list1[i][0])
#读取第二个csv,生成以name名字为元素的list4
progressbar(40, 100)
with open('D:\data2.csv','r') as f1:
readers1=csv.reader(f1)
list3=[row for row in readers1]
list4=[]
for j in range(len(list3)):
list4.append(list3[j][0])
progressbar(50, 100)
# list2有,list4没有,即为,data1有,data2没有的 减少的
ret1 = [i for i in list2 if i not in list4]
# list4没有,list2有,即为,data1没有,data2有,增加的
ret2 = [i for i in list4 if i not in list2]
#list2有,list4也有,即为相同的部分
ret3=[i for i in list2 if i in list4]
#a11 是list1相同部分大list,b11是相同部分大list
file = open('D:\diff/ret1.txt', 'w')
for i in ret1:
file.write(str(i) +','+ '\n')
file.close()
file = open('D:\diff/ret2.txt', 'w')
for i in ret2:
file.write(str(i) +','+ '\n')
file.close()
a11=zhaozuzhi(list1,ret3)
b11=zhaozuzhi(list3,ret3)
file = open('D:\diff/temp1.txt', 'w')
for i in a11:
file.write(str(i) +','+ '\n')
file.close()
file = open('D:\diff/temp2.txt', 'w')
for i in b11:
file.write(str(i) +','+ '\n')
file.close()
lines=open('D:\diff/temp1.txt').readlines()
fp=open('D:\diff/temp1.csv', 'w')
for s in lines:
fp.write( s.replace('[[','').replace(']]','')) # replace是替换,write是写入
fp.close() # 关闭文件
progressbar(70, 100)
lines=open('D:\diff/temp2.txt').readlines()
fp=open('D:\diff/temp2.csv', 'w')
for s in lines:
fp.write( s.replace('[[','').replace(']]','')) # replace是替换,write是写入
fp.close() # 关闭文件
with open('D:\diff/temp1.csv','r') as f:
readers1=csv.reader(f)
a11=[row for row in readers1]
#print(list1)
# list2=[]
# for i in range(len(list1)):
#
list2.append(list1[i][0])
with open('D:\diff/temp2.csv','r') as f:
readers1=csv.reader(f)
b11=[row for row in readers1]
#print(list2)
# print(len(a11))
# print(len(b11))
#四个name,空列表,用来放找出不同的name
name1=[]
name2=[]
name3=[]
name4=[]
#写initiavalue,形成一个txt,读的内容不对,读的内容应该是表以表2的相同部分
progressbar(80, 100)
initiavalue1=[]
initiavalue2=[]
for z in range(len(a11)):
if a11[z][1]!=b11[z][1]:
name1.append(a11[z][0])
initiavalue1.append(a11[z][1])
initiavalue2.append(b11[z][1])
a=walk_list(name1,initiavalue1,initiavalue2)
file = open('D:\diff/initiavalue.txt', 'w')
for i in a:
file.write(str(i) +','+ '\n')
file.close()
lines=open('D:\diff/initiavalue.txt').readlines()
fp=open('D:\diff/initiavalue.csv', 'w')
for s in lines:
fp.write( s.replace('[','').replace(']','')) # replace是替换,write是写入
fp.close() # 关闭文件
##写BachmannAddress,形成一个txt
BachmannAddress1=[]
BachmannAddress2=[]
for i in range(len(a11)):
if a11[i][2]!=b11[i][2]:
name2.append(str(a11[i][0]))
BachmannAddress1.append(str(a11[i][2]))
BachmannAddress2.append(str(b11[i][2]))
b=walk_list(name2,BachmannAddress1,BachmannAddress2)
file = open('D:\diff/BachmannAddress.txt', 'w')
for i in b:
file.write(str(i) +','+ '\n')
file.close()
lines=open('D:\diff/BachmannAddress.txt').readlines()
fp=open('D:\diff/BachmannAddress.csv', 'w')
for s in lines:
fp.write( s.replace('[','').replace(']','')) # replace是替换,write是写入
fp.close() # 关闭文件
# #写CalibrationExpression,形成一个txt
CalibrationExpression1=[]
CalibrationExpression2=[]
for i in range(len(a11)):
if a11[i][3]!=b11[i][3]:
name3.append(str(a11[i][0]))
CalibrationExpression1.append(str(a11[i][3]))
CalibrationExpression2.append(str(b11[i][3]))
c=walk_list(name3,CalibrationExpression1,CalibrationExpression2)
file = open('D:\diff/CalibrationExpression.txt', 'w')
for i in c:
file.write(str(i) +','+ '\n')
file.close()
lines=open('D:\diff/CalibrationExpression.txt').readlines()
fp=open('D:\diff/CalibrationExpression.csv', 'w')
for s in lines:
fp.write( s.replace('[','').replace(']','')) # replace是替换,write是写入
fp.close() # 关闭文件
#写IOInitialValue,形成一个txt
try:
IOInitialValue1=[]
IOInitialValue2=[]
for i in range(len(a11)):
if a11[i][4]!=b11[i][4]:
name4.append(a11[i][0])
IOInitialValue1.append(str(a11[i][4]))
IOInitialValue2.append(str(b11[i][4]))
d=walk_list(name4,IOInitialValue1,IOInitialValue2)
file = open('D:\diff/IOInitialValue.txt', 'w')
for i in d:
file.write(str(i) +','+ '\n')
file.close()
lines=open('D:\diff/IOInitialValue.txt').readlines()
fp=open('D:\diff/IOInitialValue.csv', 'w')
for s in lines:
fp.write( s.replace('[','').replace(']','')) # replace是替换,write是写入
fp.close() # 关闭文件
except IndexError as e:
pass
os.remove('D:\diff/temp1.txt')
os.remove('D:\diff/temp1.csv')
os.remove('D:\diff/temp2.txt')
os.remove('D:\diff/temp2.csv')
os.remove('D:\diff/initiavalue.txt')
os.remove('D:\diff/BachmannAddress.txt')
os.remove('D:\diff/CalibrationExpression.txt')
os.remove('D:\diff/IOInitialValue.txt')
os.remove('D:/data1.csv')
os.remove('D:/data2.csv')
progressbar(100,100)

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:武汉网站设计 http://www.45qun.com


  • 上一篇:利用Python中unittest实现简单的单元测试方法介绍
  • 下一篇:最后一页
  • 
    COPYRIGHT © 2015 长春市隆兴伟业物流有限公司 ALL RIGHTS RESERVED.
    本站所有原创信息,未经许可请勿任意转载或复制使用 网站地图 技术支持:肥猫科技
    精彩专题:网站建设
    购买本站友情链接、项目合作请联系客服QQ:2500-38-100