gpt4 book ai didi

python - 如何在 python 中使用电子表格或等效项来存储列表?

转载 作者:行者123 更新时间:2023-11-28 21:33:20 25 4
gpt4 key购买 nike

我正在寻找一种方法将我的列表存储在电子表格中,以便我可以读取以检查帐户是否存在以及写入以便创建新帐户。

下面的示例仅包含用户名和密码,但我还在代码的创建帐户部分中要求提供电子邮件,以便也将其存储在电子表格中。

此外,我很好奇是否有人有任何关于使其更安全的提示。

userlist = ["user1","user2","user3"]
passlist = ["pass1","pass2","pass3"]

username = str(input("\n[Login to an existing account]\nUsername:"))
password = str(input("Password:"))
if username in userlist:
if password == passlist[userlist.index(username)]:
logged_in = True

最佳答案

Hunter,如果您真的想使用电子表格来做这类事情,请查看 XlsxWriterOpenPyXL ,但请考虑使用我们在评论中讨论的“真实”数据库!

来自 OpenPyXl 文档:

从工作表中读取

from openpyxl import load_workbook
wb = load_workbook(filename='large_file.xlsx', read_only=True)
ws = wb['big_data']

for row in ws.rows:
for cell in row:
print(cell.value)

使用上面的方法,您应该能够通过密码检查执行类似的操作:

from openpyxl import load_workbook
wb = load_workbook(filename='userpasslist.xlsx', read_only=True)
ws = wb['userpassworksheet']

for row in ws.rows:
# assuming 1st column contains user names
if row[0].value == username:
# assuming 2nd column contains matching password
if row[1].value == password:
print("Success! Password matches! Do something here!")
else:
print("booo! Wrong password! Now git' out!")
break

From the XlsxWriter docs: Writing to a sheet

如何应用此功能:

import xlsxwriter

# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook('userpassbook.xlsx')
worksheet = workbook.add_worksheet()

# Some data we want to write to the worksheet.
users = (
['user1', 'password'],
['user2', 'password'],
['user3', 'password'],
['user4', 'password']
)

# Start from the first cell. Rows and columns are zero indexed.
row = 0
col = 0

# Iterate over the data and write it out row by row.
for username, password in (users):
worksheet.write(row, col, username)
worksheet.write(row, col + 1, password)
row += 1

workbook.close()

关于python - 如何在 python 中使用电子表格或等效项来存储列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54727217/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com