2017-10-21 2 views
0

私は、リストされた情報を含む既存のcsvファイルを使用してユーザがログインできるようにするプログラムを作成しようとしています。ファイルを読み取って内部に保存されているユーザー名とパスワードを見つけるプログラムを取得するにはどうすればよいですか?PythonからCSVへの行をどのように比較すればよいですか?

login = input("To sign in type 1. To register a new account type 2: ") 
if login == "1": 
    log_username = True 
    while log_username == True: 
     user = input("Username: ") 
     user_password = input("Password: ") 
     saved_users = open(user+".csv","r") 
     for line1 in saved_users: 
      line1.split(",") 
      for line2 in saved_users: 
       line2.split(",") 
       if user == saved_users: 
        if user_password == saved_users: 
          print("Login successful...") 
        else: 
         print("Incorrect password") 
         log_username == False 
       else: 
        print("Incorrect username, please try again.") 
        log_username == False 

Example of csv

+0

あなたが使用する必要がありますCSVモジュールを自分で分けようとするのではなく、 – gommb

+1

使用しているCSVファイルの設定例を表示できますか? – gommb

+0

@gommb iveは、上記の質問の例を添付しました。 –

答えて

0

をこれは、あなたのために働く必要があります(これは何繰り返しユーザ名がない意味していることに注意してください):以下は、私が実行しようとしているコードがある

import csv 
import re 

user = input("Username: ") 
user_password = input("Password: ") 

with open('test.csv', 'r') as saved_users: 
    reader = csv.reader(saved_users) 
    for line in list(reader)[1:]: # I start at 1 to skip the column labels 
     line = [re.sub('[\[\]\']', '', item) for item in line] # This cleans up the brackets and quotes from the csv file 
     if line[0] == user and line[1] == user_password: 
      print("Login successful...") 
      break 
    else: 
     print("Incorrect password") 
関連する問題