我想只允许一个特定用户通过python删除csv中其他用户的记录



程序从登录部分开始,如果只选择用户作为主管管理员,则用户可以删除其他帐户。否则,这是不允许的。

这是登录部分:

ad=input("Enter the ADMIN name:")
pwd=input("enter the Passward:")
df=pd.read_csv("admin_list.csv")
df1=df[(df['Admin']==ad) & (df['Password']==pwd)]
if(df1.empty):
print("=X= TRY AGAIN =X=")
else:

这是我的删除代码。但是,它给出了ValueError:

if (df1['Admin']== "Jhanvi"):
print(" ")
add=input("Enter Admin you want to Delete: ")
df2=pd.read_csv('admin_list.csv')
df3=df2[df2['Admin']==add]
if(df3.empty):
print("---This Admin already not exists---")
else:
data=(df2[df2['Admin']==add].index.values)
df2=df2.drop(data)
df2.to_csv('admin_list.csv',index=False)
df2=pd.read_csv('admin_list.csv')
print("-- ", add, " record is successfully deleted. --")
else:
print("Sorry you are not allowed to remove staff details")

ValueError如下所示:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

并且在这一行中上升

if (df1['Admin'] == "Jhanvi"):

解决方案:df1['Admin']替换为变量ad。当前登录的用户存储在ad中,而df1['Admin']包含所有用户名。

以下代码对我有效:

import pandas as pd
if __name__ == '__main__':
df1 = pd.read_csv("admin_list.csv")
ad = input("Enter the ADMIN name:")
# login part omitted

if ad == "Jhanvi": # this is the line that needs to be changed
print(" ")
add = input("Enter Admin you want to Delete: ")
df2 = pd.read_csv('admin_list.csv')
df3 = df2[df2['Admin'] == add]
if (df3.empty):
print("---This Admin already not exists---")
else:
data = (df2[df2['Admin'] == add].index.values)
df2 = df2.drop(data)
df2.to_csv('admin_list.csv', index=False)
df2 = pd.read_csv('admin_list.csv')
print("-- ", add, " record is successfully deleted. --")
else:
print("Sorry you are not allowed to remove staff details")

不过要提醒一句:S3DEV在评论中的建议是正确的。您不应将此类数据存储在csv文件中,而应使用适当的数据库。SQLite一开始就足够了。

更重要的是:您应该永远不要将密码存储为纯文本。至少存储他们的sha256哈希。当然,更好的做法是,存储一个哈希加一个随机盐。

相关内容

  • 没有找到相关文章

最新更新