Python compares the differences of two Excel tables

Source: Internet
Author: User
Tags diff

A colleague has two Excel tables to compare the differences, find a bit of related software, such as: Beyond Compare, Excel compare

But these two seem to be directly sorted and then compared.


Then there is no way to toss a script with Python. This script first reads the table in the file to be compared. When reading, if there is no empty line, add it with the front of it, until there is a blank line. In this case, you can not get a specific line of difference, there is only a approximate position. If there are fewer empty rows in the middle of the table, the more accurate.

#!/usr/bin/python#-*- coding:utf-8 -*-import xlrd, os, sysif len (SYS.ARGV)   != 3:  print  "\n        ./script diff_1.xlsx  diff_2.xlsx \n "  sys.exit () diff = {}xls1 = sys.argv[1]xls2 =  sys.argv[2]if os.path.isfile (XLS1)  and os.path.isfile (XLS2):  passelse:   print  "\n        ./script diff_1.xlsx diff_2.xlsx  \n "  sys.exit () def getcontent (table):   #获取xlsx表内容   tmp_data =  {}  tmp_table =  '   all_data = {}  for j in  xrange (table.nrows):     tmp = table.row_values (j)      If list (SET (TMP))  == ["]:      tmp_data[tmp_table] = " '       if tmp_table !=  ":         all_data[j]  = tmp_table      tmp_table =  '     else :      tmp2 =  ""       for i in  tmp:        try:           tmp2 = tmp2 + i +  ","          except:          tmp2 = tmp2 + str (i) +  ","       tmp_table = tmp_table + tmp2 +  "\ n"    #把多行的内容放一起   return  (tmp_data, all_data) def write_file (Excel_diff,  filename):   f = open (filename,  ' W ')   f.write (Excel_diff)    F.close () Def diff_content (table1,table2):  #检查两个表差异   diff_tmp = []  for i in  table1:    if i in table2:      pass     else:      diff_tmp.append (i)   return list (set ( diff_tmp)  def get_rows (diff, all_data):  #获取差异位置   tmp = []   for i in diff:    for j in all_data:       if all_data[j].strip ()  == i.strip ():         tmp.append (j)         break  return tmp        for i in range (0,2):  #  compare several tables    Data1 = xlrd.open_workbook (XLS1)   table1 = data1.sheets () [I]  data2  = xlrd.open_workbooK (XLS2)   table2 = data2.sheets () [i]    tmp1, all1 =  GetContent (table1)   tmp2, all2 = getcontent (table2)   result =  Diff_content (TMP1, TMP2)   #1   with  2  no specific content #  result2= diff_content (TMP2,  TMP1)    #2   with  1  no specific content   diff[i] = get_rows (RESULT,ALL1)   print sorted (Diff[i]) #  write_file (sorted (diff[i]),  "/tmp/diff.txt") #   diff[i] = get_rows (RESULT2,ALL2) #  print sorted (Diff[i])


Python compares the differences of two Excel tables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.