gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:18:25 26 4
gpt4 key购买 nike

我正在制作一个 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 文件时,输出的“差异”似乎存在于两个文件中。有没有人有任何帮助我的建议?任何东西都会很棒谢谢

最佳答案

也许你可以这样做:

  1. 更改:for row in all_ws[1].iter_cols():

    To: for row in all_ws[0].iter_cols():

  2. 之后:

更改 if cell.value not in sheet_cols:

到:if cell2.value in sheet_cols:

希望对您有所帮助:)

关于python - Openpyxl比较两个工作表中的值以查看值是否不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51968628/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com