【问题标题】:Want to check certain values are present in the excel sheet with python想用python检查excel表中是否存在某些值
【发布时间】:2022-01-18 12:49:29
【问题描述】:

我有一个 Excel 表格,我想检查是否存在特定值

代码是

import openpyxl

book = openpyxl.load_workbook("C:\\Users\\abdul_saboor\\PycharmProjects\\sleneiumpythonsession\\venv\\LoginData.xlsx")
first_sheet = book.get_sheet_by_name('Sheet1')
particular_cell_value = first_sheet.rows
particular_cell_value1 = first_sheet.columns
for x in  particular_cell_value:
    for j in particular_cell_value1:
        if "adm@yourstore.com" in particular_cell_value1:
            print("Test case passed")
        else:
            print("Failed")

但是每次输出都显示失败?我想检查一下 adm@yourstore.com 是否存在。有人可以帮忙吗?

【问题讨论】:

    标签: python pandas automation


    【解决方案1】:

    使用pandas:

    import pandas as pd
    
    df = pd.read_excel('LoginData.xlsx')
    
    isin = 'adm@yourstore.com' in df['username'].tolist()
    
    print(f"User adm@yourstore.com is in file: {'yes' if isin else 'no'}")
    

    【讨论】:

    • 如果我想要参数化它 print(f""+ valuetocheck +" is in file:"+{'yes' if isin else 'no'}+"").. 将 valuetocheck 传递给他函数 def checkstring(valuetocheck,str_columnname,file_path): df = pd.read_excel(file_path) is_present = valuetocheck in df[str_columnname].tolist() print(f""+ valuetocheck +" is in file:"+{'yes' if isin else 'no'}+"") valuetocheck="" str_columnname="" file_path='' checkstring(valuetocheck,str_columnname,file_path) NameError: name 'isin' is not defined
    猜你喜欢
    • 2017-08-22
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2011-10-11
    • 2011-04-09
    • 1970-01-01
    相关资源
    最近更新 更多