如何检查在python中输入的内容是否与csv文件中的内容相同

时间:2017-04-03 13:49:59

标签: python csv input

我正在尝试创建一个程序,以查看输入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)

以上是迄今为止的代码。

请帮忙,

提前致谢

1 个答案:

答案 0 :(得分: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]