PYTHON(PYQT) - CSV - 按行阅读

时间:2017-09-02 13:22:44

标签: python pyqt pyqt5

我在行记事本中有user;password;fullname

当我将表单放在行小部件上时,我的表单应该只接受用户和密码,但每次运行时,我的表单都会退出。

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!")

1 个答案:

答案 0 :(得分: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!")