2017-04-03 91 views
-1

我試圖做一個程序,看是否進入了一個Python程序的用戶名和密碼與存儲在用戶名和密碼一個csv文件。我如何檢查是否在Python中輸入了什麼東西是一樣的CSV文件的東西

logorsign = input("""Choose one: 
1. Log in 
2. Sign up 
3. Quit 
""") 
print("") 

if logorsign == "1":   
    Netflix_CSV = csv.reader(open("Netflix Task.csv", "rt"))  
    first_Line = next(Netflix_CSV) 
    checkuser = input("Enter Username: ") 
    print("") 
    checkpasw = input("Enter Password: ") 
    for row in Netflix_CSV: 
     if row[0] == checkuser: 
      print(watched_films) 

以上是迄今爲止的代碼。

請幫幫忙,

在此先感謝

+0

請看看熊貓,這將改善您的查詢somewaht –

回答

0

最好的數據結構,這裏用的是一本字典:

user_passwords = dict() 
user_films = dict() 
user_logged_in = False 

given_username = input('Enter Username: ') 
print('') 
given_password = input('Enter Password: ') 
print('') 

with open('Netflix Task.csv', 'r') as fh: 
    reader = csv.reader(fh) 
    reader.next() # Skip the header line 
    for line in reader: 
     username, password, watched_films = line 
     user_passwords[username] = password 
     user_films[username] = watched_films 

if user_passwords.get(given_username, False) == given_password: 
    print('login successful') 
    user_logged_in = True 
else: 
    print('Bad username/password') 

然後,訪問用戶的電影:

users_films = user_films[username] 
+0

我得到這個錯誤:回溯(最近通話最後一個): 文件 「N:\任務2.py」 28行,在 與csv.reader(開放( 'Netflix的Task.csv', 'RT'))爲FH: AttributeError的:__exit__ –

+0

更改了文件處理程序 – Rowshi

相關問題