簡體   English   中英

我試圖用 python 做一個白名單檢查器

[英]Im trying to make a whitelist checker with python

我是 python 的新手,我有一張 excel 表,第一列中有名稱,我想制作一個 python 代碼,用戶將在其中輸入名稱,腳本將檢查名稱是否在列中。 我能夠打印出列中的行,但我不確定如何在循環中一一檢查。

import pandas as pd
from openpyxl import load_workbook

book = load_workbook('Desktop\python\excel.xlsx')
sheet = book['Sheet1']

for row in sheet.rows:
    whitelist = row[0].value
    print (whitelist)

user_input = input(message)

if user_input == whitelist:
    print("User is whitelisted")
else:
    print("The user is not whitelisted")

您不會將名稱存儲在任何地方。 將它們放在一組中,然后檢查很容易。

import pandas as pd
from openpyxl import load_workbook

book = load_workbook('Desktop\python\excel.xlsx')
sheet = book['Sheet1']

whitelist = set(row[0].value for row in sheet.rows)

user_input = input(message)

if user_input in whitelist:
    print("User is whitelisted")
else:
    print("The user is not whitelisted")

很簡單,只要寫if inside for like this

# Ask the user to input  the name
user_input = input("Enter name: ")

for row in sheet.rows:
    # now check that one by one 
    if user_input == row[0].value:
       print("User is whitelisted")

暫無
暫無

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

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