在CSV文件中搜索词不起作用

时间:2018-11-19 01:34:43

标签: python csv python-3.7

一个按钮可以激活以下功能,但是它似乎不起作用,有人知道为什么吗?

        with open (r"D:\Users\gblmac\Desktop\Visitor GUI\Site Visitors.csv") as csvread:
        search_term=self.textEdit_Search.toPlainText()
        print(search_term)
        search=csv.reader(csvread)
        for row in search:
            if search_term in row:
                print(row)

完整代码:

import sys
import csv
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):

    def quit_visitor(self):
        print("I quit!")
        sys.exit()

    def add_visitor(self):
        print("Added!")

        add_Date=str(int(self.dateEdit_Date.date().day()))+str("/")+str(int(self.dateEdit_Date.date().month()))+str("/")+str(int(self.dateEdit_Date.date().year()))
        add_Name=self.textEdit_Name.toPlainText()
        add_Company=self.textEdit_Company.toPlainText()
        add_Job=self.textEdit_Job.toPlainText()
        add_Originator=self.textEdit_Originator.toPlainText()
        add_Induction=str(self.comboBox_Induction.currentText())
        add_RAMS=str(self.comboBox_RAMS.currentText())
        add_Permit=str(self.comboBox_Permit.currentText())
        add_MSDS=str(self.comboBox_MSDS.currentText())
        add_Comments=self.textEdit_Comments.toPlainText()

        with open("Site Visitors.csv", "a", newline="") as csvfile:
            visitor_contents=csv.writer(csvfile)
            visitor_contents.writerow([add_Date, add_Name, add_Company, add_Job, add_Originator, add_Induction, add_RAMS, add_Permit, add_MSDS, add_Comments])

        print(add_Date)
        print(add_Name)
        print(add_Company)
        print(add_Job)
        print(add_Originator)
        print(add_Induction)
        print(add_RAMS)
        print(add_Permit)
        print(add_MSDS)
        print(add_Comments)       

    def search_visitor(self):

        with open (r"D:\Users\gblmac\Desktop\Visitor GUI\Site Visitors.csv") as csvread:
            search_term=self.textEdit_Search.toPlainText()
            print(search_term)
            search=csv.reader(csvread)
            for row in search:
                for fielf in row:
                    if search_term in field:
                        print(row)


    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(738, 837)
        self.gridLayoutWidget = QtWidgets.QWidget(Form)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(30, 20, 701, 761))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout_Visitor = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout_Visitor.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_Visitor.setObjectName("gridLayout_Visitor")
        self.textEdit_Name = QtWidgets.QTextEdit(self.gridLayoutWidget)
        self.textEdit_Name.setMaximumSize(QtCore.QSize(16777215, 50))
        font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.textEdit_Name.setFont(font)
    self.textEdit_Name.setObjectName("textEdit_Name")
    self.gridLayout_Visitor.addWidget(self.textEdit_Name, 1, 2, 1, 1)
    self.label_Date = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_Date.setFont(font)
    self.label_Date.setObjectName("label_Date")
    self.gridLayout_Visitor.addWidget(self.label_Date, 0, 0, 1, 1)
    self.textEdit_Job = QtWidgets.QTextEdit(self.gridLayoutWidget)
    self.textEdit_Job.setMaximumSize(QtCore.QSize(16777215, 150))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.textEdit_Job.setFont(font)
    self.textEdit_Job.setObjectName("textEdit_Job")
    self.gridLayout_Visitor.addWidget(self.textEdit_Job, 3, 2, 1, 1)
    self.label_Name = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_Name.setFont(font)
    self.label_Name.setObjectName("label_Name")
    self.gridLayout_Visitor.addWidget(self.label_Name, 1, 0, 1, 1)
    self.textEdit_Originator = QtWidgets.QTextEdit(self.gridLayoutWidget)
    self.textEdit_Originator.setMaximumSize(QtCore.QSize(16777215, 50))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.textEdit_Originator.setFont(font)
    self.textEdit_Originator.setObjectName("textEdit_Originator")
    self.gridLayout_Visitor.addWidget(self.textEdit_Originator, 4, 2, 1, 1)
    self.dateEdit_Date = QtWidgets.QDateEdit(self.gridLayoutWidget)
    self.dateEdit_Date.setMaximumSize(QtCore.QSize(16777215, 100))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(28)
    self.dateEdit_Date.setFont(font)
    self.dateEdit_Date.setAlignment(QtCore.Qt.AlignCenter)
    self.dateEdit_Date.setCalendarPopup(True)
    self.dateEdit_Date.setObjectName("dateEdit_Date")
    self.gridLayout_Visitor.addWidget(self.dateEdit_Date, 0, 2, 1, 1)
    self.label_Comments = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_Comments.setFont(font)
    self.label_Comments.setObjectName("label_Comments")
    self.gridLayout_Visitor.addWidget(self.label_Comments, 9, 0, 1, 1)
    self.textEdit_Comments = QtWidgets.QTextEdit(self.gridLayoutWidget)
    self.textEdit_Comments.setMaximumSize(QtCore.QSize(16777215, 150))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.textEdit_Comments.setFont(font)
    self.textEdit_Comments.setObjectName("textEdit_Comments")
    self.gridLayout_Visitor.addWidget(self.textEdit_Comments, 9, 2, 1, 1)
    self.label_Job = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_Job.setFont(font)
    self.label_Job.setObjectName("label_Job")
    self.gridLayout_Visitor.addWidget(self.label_Job, 3, 0, 1, 1)
    self.label_Originator = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_Originator.setFont(font)
    self.label_Originator.setObjectName("label_Originator")
    self.gridLayout_Visitor.addWidget(self.label_Originator, 4, 0, 1, 1)
    self.textEdit_Company = QtWidgets.QTextEdit(self.gridLayoutWidget)
    self.textEdit_Company.setMaximumSize(QtCore.QSize(16777215, 50))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.textEdit_Company.setFont(font)
    self.textEdit_Company.setObjectName("textEdit_Company")
    self.gridLayout_Visitor.addWidget(self.textEdit_Company, 2, 2, 1, 1)
    self.label_Company = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_Company.setFont(font)
    self.label_Company.setObjectName("label_Company")
    self.gridLayout_Visitor.addWidget(self.label_Company, 2, 0, 1, 1)
    self.label_Induction = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_Induction.setFont(font)
    self.label_Induction.setWordWrap(True)
    self.label_Induction.setObjectName("label_Induction")
    self.gridLayout_Visitor.addWidget(self.label_Induction, 5, 0, 1, 1)
    self.label_RAMS = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_RAMS.setFont(font)
    self.label_RAMS.setWordWrap(True)
    self.label_RAMS.setObjectName("label_RAMS")
    self.gridLayout_Visitor.addWidget(self.label_RAMS, 6, 0, 1, 1)
    self.label_Permit = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_Permit.setFont(font)
    self.label_Permit.setWordWrap(True)
    self.label_Permit.setObjectName("label_Permit")
    self.gridLayout_Visitor.addWidget(self.label_Permit, 7, 0, 1, 1)
    self.label_MSDS = QtWidgets.QLabel(self.gridLayoutWidget)
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    font.setBold(True)
    font.setUnderline(True)
    font.setWeight(75)
    self.label_MSDS.setFont(font)
    self.label_MSDS.setWordWrap(True)
    self.label_MSDS.setObjectName("label_MSDS")
    self.gridLayout_Visitor.addWidget(self.label_MSDS, 8, 0, 1, 1)
    self.comboBox_Induction = QtWidgets.QComboBox(self.gridLayoutWidget)
    self.comboBox_Induction.setMaximumSize(QtCore.QSize(100, 16777215))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.comboBox_Induction.addItem("Yes")
    self.comboBox_Induction.addItem("No")
    self.comboBox_Induction.addItem("N/A")
    self.comboBox_Induction.setFont(font)
    self.comboBox_Induction.setCurrentText("")
    self.comboBox_Induction.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
    self.comboBox_Induction.setObjectName("comboBox_Induction")
    self.gridLayout_Visitor.addWidget(self.comboBox_Induction, 5, 2, 1, 1)
    self.comboBox_RAMS = QtWidgets.QComboBox(self.gridLayoutWidget)
    self.comboBox_RAMS.setMaximumSize(QtCore.QSize(100, 16777215))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.comboBox_RAMS.addItem("Yes")
    self.comboBox_RAMS.addItem("No")
    self.comboBox_RAMS.addItem("N/A")
    self.comboBox_RAMS.setFont(font)
    self.comboBox_RAMS.setCurrentText("")
    self.comboBox_RAMS.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
    self.comboBox_RAMS.setObjectName("comboBox_RAMS")
    self.gridLayout_Visitor.addWidget(self.comboBox_RAMS, 6, 2, 1, 1)
    self.comboBox_Permit = QtWidgets.QComboBox(self.gridLayoutWidget)
    self.comboBox_Permit.setMaximumSize(QtCore.QSize(100, 16777215))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.comboBox_Permit.addItem("Yes")
    self.comboBox_Permit.addItem("No")
    self.comboBox_Permit.addItem("N/A")
    self.comboBox_Permit.setFont(font)
    self.comboBox_Permit.setCurrentText("")
    self.comboBox_Permit.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
    self.comboBox_Permit.setObjectName("comboBox_Permit")
    self.gridLayout_Visitor.addWidget(self.comboBox_Permit, 7, 2, 1, 1)
    self.comboBox_MSDS = QtWidgets.QComboBox(self.gridLayoutWidget)
    self.comboBox_MSDS.setMaximumSize(QtCore.QSize(100, 16777215))
    font = QtGui.QFont()
    font.setFamily("Arial")
    font.setPointSize(14)
    self.comboBox_MSDS.addItem("Yes")
    self.comboBox_MSDS.addItem("No")
    self.comboBox_MSDS.addItem("N/A")
    self.comboBox_MSDS.setFont(font)
    self.comboBox_MSDS.setCurrentText("")
    self.comboBox_MSDS.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
    self.comboBox_MSDS.setObjectName("comboBox_MSDS")
    self.gridLayout_Visitor.addWidget(self.comboBox_MSDS, 8, 2, 1, 1)
    self.pushButton_Quit = QtWidgets.QPushButton(Form)
    self.pushButton_Quit.setGeometry(QtCore.QRect(650, 800, 75, 23))
    self.pushButton_Quit.setObjectName("pushButton_Quit")
    self.pushButton_Quit.clicked.connect(self.quit_visitor)
    self.pushButton_Search = QtWidgets.QPushButton(Form)
    self.pushButton_Search.setGeometry(QtCore.QRect(490, 800, 75, 23))
    self.pushButton_Search.setObjectName("pushButton_Search")
    self.pushButton_Search.clicked.connect(self.search_visitor)
    self.pushButton_Add = QtWidgets.QPushButton(Form)
    self.pushButton_Add.setGeometry(QtCore.QRect(570, 800, 75, 23))
    self.pushButton_Add.setObjectName("pushButton_Add")
        self.pushButton_Add.clicked.connect(self.add_visitor)
        self.textEdit_Search = QtWidgets.QTextEdit(Form)
        self.textEdit_Search.setGeometry(QtCore.QRect(193, 790, 291, 41))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(14)
        self.textEdit_Search.setFont(font)
        self.textEdit_Search.setLineWidth(3)
        self.textEdit_Search.setMidLineWidth(0)
        self.textEdit_Search.setObjectName("textEdit_Search")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label_Date.setText(_translate("Form", "Date:"))
        self.label_Name.setText(_translate("Form", "Name:"))
        self.label_Comments.setText(_translate("Form", "Comments:"))
        self.label_Job.setText(_translate("Form", "Job/Task:"))
        self.label_Originator.setText(_translate("Form", "Originator:"))
        self.label_Company.setText(_translate("Form", "Company:"))
        self.label_Induction.setText(_translate("Form", "Induction Completed:"))
        self.label_RAMS.setText(_translate("Form", "RAMS Supplied:"))
        self.label_Permit.setText(_translate("Form", "Permit Required:"))
        self.label_MSDS.setText(_translate("Form", "MSDS Supplied:"))
        self.comboBox_Induction.setToolTip(_translate("Form", "Select Yes, No or N/A"))
        self.comboBox_RAMS.setToolTip(_translate("Form", "Select Yes, No or N/A"))
        self.comboBox_Permit.setToolTip(_translate("Form", "Select Yes, No or N/A"))
        self.comboBox_MSDS.setToolTip(_translate("Form", "Select Yes, No or N/A"))
        self.pushButton_Quit.setText(_translate("Form", "Quit"))
        self.pushButton_Search.setText(_translate("Form", "Search"))
        self.pushButton_Add.setText(_translate("Form", "Add"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(Form)
    Form.show()
    sys.exit(app.exec_())

csv文件如下:

1/1/2000,a,a,a,a,是,是,是,是

2000年1月1日,b,b,b,b,是,是,是,是,b

2000年1月1日,莱斯利·麦金托什(Leslie Mackintosh),范德兰德(Vanderlande),工作中,莱斯马克(Les Mack),是,是,否,不适用,无!

2000年1月1日,斯蒂芬·马什(Stephen Marsh),范德兰德(Vanderlande),工作中,莱斯马克(Les Mack),是,是,否,不适用,无!

2000年1月1日,伊恩·哈顿(Ian Hatton),范德兰德(Vanderlande),工作中,莱斯马克(Les Mack),是,是,是,否,不适用,无!

2000年1月1日,西蒙·米尔斯,范德兰德,工作,西蒙·米尔斯,是,是,否,不适用,无!

因此,如前所述,如果搜索词是“ Leslie”,我希望可以打印出第三行,但不会。

搜索词的打印方式如下:

    search_term=self.textEdit_Search.toPlainText()
    print(search_term)

但是我随后收到“ Python停止工作”的错误消息,什么也没打印。

1 个答案:

答案 0 :(得分:0)

使用AlexanderReynolds suggestion in the comments标识行中的术语,然后我们可以打印整行:

def search_visitor(self):
    with open (r"Site Visitors.csv") as csvread:
        search_term=self.textEdit_Search.toPlainText().lower()
        search=csv.reader(csvread)
        for n, row in enumerate(search, 1):
            for col in row:
                if search_term in col.lower():
                    print('Line {}: {}'.format(n, ' '.join(row)))

确认在第3行找到了搜索词:

Line 3: 1/1/2000 Leslie Mackintosh Vanderlande Working Les Mack Yes Yes No N/A None!