![](/img/trans.png)
[英]Reading file line by line with Python in multithreading is reading only the first line
[英]Python only reading the first line of CSV file
我相信這很簡單,但它讓我發瘋。 代碼僅讀取 CSV 文件的第一行。 如果我在 CSV 頂部輸入名稱,它會要求輸入密碼。 如果我輸入列表中的任何其他人,它會轉到“您當前未注冊”。 我想不通為什么?
with open('members.csv', 'r') as mem_login:
login_reader = csv.reader(mem_login)
for row in login_reader:
if member in row:
password = input('Please enter password... ').lower()
if password in row:
print('Welcome ' + member.capitalize() + ', login successful!')
sys.exit()
else:
reg_now = input('You are not currently registered. Would you like to sign-up now? Y or N ').lower()
您的else
案例附加到if member in row:
,因此如果第一row
不包含member
,您會立即假定它們未注冊。 Dedent 一行以將其附加到for
循環,因此else
塊僅在for
循環運行完成而沒有break
才會觸發:
# newline='' is mandatory for csv module to handle dialect's newline conventions
# mode defaults to 'r', so you don't need to use it
with open('members.csv', newline='') as mem_login:
# Unpack to username and userpass, and test individually,
# so user names are not their own passwords
for username, userpass in csv.reader(mem_login): # One-lined creation of reader since it's only needed for loop
if member == username:
password = input('Please enter password... ').lower()
if password == userpass:
print('Welcome ' + member.capitalize() + ', login successful!')
break # break bypasses the for's else block
else:
# Only runs if break not executed
reg_now = input('You are not currently registered. Would you like to sign-up now? Y or N ').lower()
我做了一些其他小的修復/改進(所有評論),但最大的變化是else
塊的 dedent 將它附加到for
循環,而不是if
。
注意:即使經過我的更改,這也存在嚴重缺陷(無殼密碼、明文密碼),因此切勿將其用於真實用戶身份驗證。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.