[英]Im trying to make a whitelist checker with python
Im new to python and I have an excel sheet with names in the first column and I want to make a python code where a user will input a name and the script will check if the name is in the column.我是 python 的新手,我有一张 excel 表,第一列中有名称,我想制作一个 python 代码,用户将在其中输入名称,脚本将检查名称是否在列中。 I am able to print out the rows in the column but I am not sure how to check it one by one in a loop.我能够打印出列中的行,但我不确定如何在循环中一一检查。
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")
You're not storing the names anywhere.您不会将名称存储在任何地方。 Put them in a set, then checking is easy.将它们放在一组中,然后检查很容易。
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")
that a easy, just write if
inside for
like this很简单,只要写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.