简体   繁体   English

PYTHON(PYQT)-CSV-按行读取

[英]PYTHON (PYQT) - CSV - READING BY ROW

I have user;password;fullname in the notepad by row. 我在记事本中按行有user;password;fullname

My form should only accept user and password when I put it on the line widget but every time I run this, my form exits. 我的表单仅应在将其放在行小部件上时接受用户名和密码,但是每次运行此表单时,我的表单都会退出。

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

When you use csv.reader() it is not necessary to use the split() function, since internally it separates it. 使用csv.reader() ,不必使用split()函数,因为它是在内部将其分隔开的。 in the variable row you have the list of elements. 在变量行中,您具有元素列表。 So in your case the solution is as follows: 因此,根据您的情况,解决方案如下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM