简体   繁体   中英

Is there a way to align two separate Excel files so that each item in one file has a unique matching item in the other file? Code is almost working

I have two Excel files that need to be align in the sense that each row from one excel file has a unique corresponding row in the other excel file, weather it be a matching data point or a blank value. The Excel files are not the same size and have some values that match and some that do not, however, they are both in sequential order.

I am trying to accomplish this by inserting empty rows. I am having trouble inserting the correct amount of empty rows when there is no match and then continuing to the next value. I believe that my code is very close to working. Also included in the code both modified Excel files are combined into one file as separate tabs.

j=0
iterations=100+Branch_Flow_Pre.max_row
for i in range(2, iterations):
    #if str(Branch_Flow_Pre.cell(row=i, column=1).value) == "None" and str(Branch_Flow_Post.cell(row=i, column=1).value) == "None":
    #   print("blanks, i = ",i,"j = ",j)            
    #   i += 1
    if Branch_Flow_Pre.cell(row=i, column=2).value == Branch_Flow_Post.cell(row=i, column=2).value and Branch_Flow_Pre.cell(row=i, column=8).value == Branch_Flow_Post.cell(row=i, column=8).value:
        print("match, i = ",i,"j= ",j)
        i += 1
    else:
        j=0
        while j<21:
            if Branch_Flow_Pre.cell(row=i+j, column=2).value == Branch_Flow_Post.cell(row=i, column=2).value and Branch_Flow_Pre.cell(row=i+j, column=8).value == Branch_Flow_Post.cell(row=i, column=8).value:
                if j!=0:
                    for x in range(0, j+1):
                        Branch_Flow_Post.insert_rows(i)
                        print("insert Post, x = ",x,"i = ",i,"j = ",j)
                else:
                    print("error")
                i = i+j
                j=21
                break
            elif Branch_Flow_Pre.cell(row=i, column=2).value == Branch_Flow_Post.cell(row=i+j, column=2).value and Branch_Flow_Pre.cell(row=i, column=8).value == Branch_Flow_Post.cell(row=i+j, column=8).value:
                if j!=0:
                    for x in range(0, j+1):
                        Branch_Flow_Pre.insert_rows(i)
                        print("insert Pre, x = ",x,"i = ",i,"j = ",j)
                else:
                    print("error")
                i = i+j
                j=21
                break
            elif j==20:
                Branch_Flow_Post.insert_rows(i)
                print("break, i = ",i,"j = ",j," Insert Post")
                i += 1
                j = 21
                break
            else:
                print("increment, i = ",i,"j = ",j)
                j += 1
c=1
r=2
for row in Branch_Flow_Pre.values:
    for v in row:
        BF_Pre.cell(row=r, column=c).value = v
        c += 1
    c=1
    r += 1

c=1
r=2
for row in Branch_Flow_Post.values:
    for v in row:
        BF_Post.cell(row=r, column=c).value = v
        c += 1
    c=1
    r += 1

book3.save(outfilename)
## the rest is not code
desired output:
Input1  Input2  Output1  Output2
 A 1     B 2      A 1  
 B 2              B 2      B 2
 C 3     C 3       
         x y      C 3      C 3
         D 4               x y
 D 4                       
                  D 4      D 4

Actual output:
Input1  Input2  Output1  Output2
 A 1     B 2      A 1  
 B 2              B 2      B 2
 C 3     C 3       
         x y      C 3      
         D 4               
 D 4                       
                  D 4      
                           C 3
                           x y
                           D 4

I was able to correct the code by adding additional conditions that do not allow empty cells to be counted as an acceptable match. Here is an image of my working script:

working code

This function will perform a comparison between two excel tabs with similar data and align the two tabs so that each item has a unique item in the other tab. Alternatively you can think of this as a function which will place matching items on the same excel line number in each tab so that a comparison can be easily performed between the two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM