簡體   English   中英

從一個excel表的列中的單元格中獲取值,並檢查該值是否存在於其他excel表的列中

[英]Take values from cells in column of one excel table and check if this values exist in column of other excel table

這是代碼,我被困在這里。 我想做我在代碼下面描述的功能。 我需要你的幫助。 謝謝!

import openpyxl as xl

dict_store = {}

# opening the head excel file
data1 = "D:\\data1.xlsx"
wb1 = xl.load_workbook(data1)
ws1 = wb1.worksheets[0]

# opening the second excel file
data2 = "D:\\data2.xlsx"
wb2 = xl.load_workbook(data2)
ws2 = wb2.active

# calculate total number of rows
data1_max_row = ws1.max_row
data2_max_row = ws2.max_row

# I created this func to store the values of head table in dict (I don't know if that's right)
def create_dict():
    for i in range(1, data1_max_row):
        dict_store[i] = ws1.cell(i, 1).value

    # for j in dict.values():
    # print(j)


def check_cells():
    for i in range(1, data2_max_row):
        for j in dict_store.values():
            if i == j:
                print("true")


if __name__ == '__main__':
    create_dict()
    check_cells()

我有一個包含 2 列的表:

Column 1   Column 2
a          f
b          g
c          h
d          i
e          j

我有一個有 2 列的第二個表:

Column 1   Column 2
a          a
x          b
e          c

所以,我想檢查第二個表的第 1 列中的行是否存在於第 1 列的第一個表中。如果是 -> 將其他列(第 2 列)的值復制到新的第 3 列(帶有一些標題名稱)到第二個表,在同一行。 如果不是 -> 行保持為空。

更好理解的示例:第二個表中的ae存在於表 1 的第 1 列中,但它們不在一行中。 我的意思是他們是混合的。 因此,現在需要對第二個表進行添加第三列:

結果我除外:

Column 1   Column 2   Column 3
a          a          f (taked from column 2 in table 1)
x          b          this row stays empty, beacuse x doesn't exist there
e          c          j (taked value from column 2 in table 1)

我希望你理解我。 提前致謝!

使用 vlookup 不要重新發明輪子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM