python – Openpyxl比较两个表中的值以查看值是否不存在

我正在制作一个excel比较程序,它需要一定数量的工作表,比较它们并检查其中一个工作表中的值是否不存在.但是,我收到了重复的问题.为了澄清,这是我的代码:

import tkinter as tk
from tkinter import filedialog
import openpyxl,os,csv
from openpyxl.utils import get_column_letter,column_index_from_string

# Output File
outputFile = open('output.csv','w',newline='')
outputWriter = csv.writer(outputFile)

# Tk initialization for file dialog
root = tk.Tk()
root.withdraw()

# Number of sheets to be compared
number = input('Enter number of workbook sheets for comparison: ')
number = int(number)

# Functions for generating file paths
def generate_file_path():
    file_path = filedialog.askopenfilename(title="Open Workbook")
    return file_path

# Variables to store file paths,workbooks and worksheets
all_ws = []

# Core function for program's logistics
def core():
    # for loops for generating file paths,workbooks and worsheets
    for x in range(number):
        path = generate_file_path()
        wb = openpyxl.load_workbook(path)
        ws = wb['CBF']
        all_ws.append(ws)

    # for loop to use for finding diff
    for row in all_ws[1].iter_cols():
        for cellz in row:
            sheet_cols.append(cellz.value)

    # loop that checks if the value does not exist
    for ws_diff in range(number):
        for row,row2 in zip(all_ws[0].iter_cols(),all_ws[1].iter_cols()):
            for cell,cell2 in zip(row,row2):
                if cell.value not in sheet_cols:
                    outputWriter.writerow([str(cell2.value)])

但是,当我检查我的csv文件时,输出的“差异”似乎出现在两个文件上.有没有人有任何帮助我的建议?什么都会很好,谢谢你

最佳答案
也许你可以这样做:

>更改:对于all_ws中的行[1] .iter_cols():

要:for all_ws [0] .iter_cols()中的行:
>之后:

如果cell.value不在sheet_cols中,则更改:

要:如果在sheet_cols中的cell2.value:

希望能帮助到你 :)

相关文章

Python中的函数(二) 在上一篇文章中提到了Python中函数的定...
Python中的字符串 可能大多数人在学习C语言的时候,最先接触...
Python 面向对象编程(一) 虽然Python是解释性语言,但是它...
Python面向对象编程(二) 在前面一篇文章中谈到了类的基本定...
Python中的函数(一) 接触过C语言的朋友对函数这个词肯定非...
在windows下如何快速搭建web.py开发框架 用Python进行web开发...