2017-09-02 223 views
0

我在记事本中排有user;password;fullnamePYTHON(PYQT) - CSV - 按行读取

我的表单应该只接受用户和密码,当我把它放在线上的小部件,但每次我运行这个,我的表单退出。

def login_button_clicked(self): 
    import csv 
    with open('user.txt', newline='') as f: 
     reader = csv.reader(f, delimiter=';', quoting=csv.QUOTE_ALL) 
     for row in reader: 
      us, pa, fn = line.rstrip().split(';') 

      if self.username_line.text() == us and self.password_line.text() == pa: 
       QtWidgets.QMessageBox.information(self, "LOGIN", "LOGIN SUCCESSFUL!") 
       self.isLogged.emit() 
       self.close() 
       return 
      else: 
       QtWidgets.QMessageBox.information(self, "LOGIN FAILED", "LOGIN FAILED!") 

回答

0

当您使用csv.reader()没有必要使用split()功能,因为它的内部分隔它。在变量行中你有元素列表。所以在你的情况下,解决方案如下:

def login_button_clicked(self): 
    import csv 
    with open('user.txt', newline='') as f: 
     reader = csv.reader(f, delimiter=';', quoting=csv.QUOTE_ALL) 
     for row in reader: 
      us, pa, fn = row 
      # us, ps = row[:2] 
      if self.username_line.text() == us and self.password_line.text() == pa: 
       QtWidgets.QMessageBox.information(self, "LOGIN", "LOGIN SUCCESSFUL!") 
       self.isLogged.emit() 
       self.close() 
       return 
      else: 
       QtWidgets.QMessageBox.information(self, "LOGIN FAILED", "LOGIN FAILED!")