【第22期】观点:IT 行业加班,到底有没有价值?

python,比较两个xml

原创 2016年08月31日 09:26:04

实现的功能是从两个xml读取多个子节点和属性,比较相同子节点是否相同,将不同的内容保存生成csv文件。

# -*- 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)
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

使用minidom来处理XML的示例(Python 学习)(转载)

<a id="cb_post_title_url" class="p
  • cdtdx
  • cdtdx
  • 2012-03-25 17:16
  • 79

比较两个xml完成

# -*- coding: UTF-8 -*-#作用:查询表一表二的不同内容,表1相对于表2 ,表2相对于表1#data1生成名字的list2import csvimport reimport os#...

欢迎关注CSDN程序人生公众号

关注程序员生活,汇聚开发轶事。

使用Python的MD5算法比较两个文件

在Python中内置了md5的实现,就是md5模块,因此可以很简单的处理比较两个文件是否一致的问题。如以下代码片断: import os,sys,md5 <font style="background-color: rgb(255, 255

【python】比较两个json并返回差别

现在要比较两个json是否相等,若不同则返回差别比如下面数据的对比:dict1 = {"id": "50356270565167104", "name": "班级优化"} dict2 = {"id":...

使用Python的MD5算法比较两个文件

在Python中内置了md5的实现,就是md5模块,因此可以很简单的处理比较两个文件是否一致的问题。如以下代码片断: import os,sys,md5 <font style="background-color: rgb(255, 255
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)