检查元素是否不在列表中的正确方法是哪种? “不在列表中”或“不在列表中”

时间:2019-02-17 15:22:39

标签: python list element

请参见下面的代码:

import sys
import csv
import fileinput
from PyQt5.QtCore    import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *


class MyWindow(QWidget):
    def __init__(self, fileName, parent=None):
        super(MyWindow, self).__init__(parent)

        MyWindow.setGeometry(self, 0,0,800,420)

        self.fileName = fileName

        self.model = QStandardItemModel(self)

        self.tableView = QTableView(self)


        self.tableView.setModel(self.model)
        self.tableView.horizontalHeader().setStretchLastSection(True)


        self.pushButtonLoad = QPushButton(self)
        self.pushButtonLoad.setText("Load Csv File!")
        self.pushButtonLoad.clicked.connect(self.on_pushButtonLoad_clicked)

        self.pushButtonWrite = QPushButton(self)
        self.pushButtonWrite.setText("Write Csv File!")
        self.pushButtonWrite.clicked.connect(self.on_pushButtonWrite_clicked)

        self.layoutVertical = QVBoxLayout(self)
        self.layoutVertical.addWidget(self.tableView)
        self.layoutVertical.addWidget(self.pushButtonLoad)
        self.layoutVertical.addWidget(self.pushButtonWrite)

    def loadCsv(self, fileName):
        with open(fileName, "r") as fileInput:                    
            for row in csv.reader(fileInput):
                items = [
                    QStandardItem(field)
                    for field in row
                ]
                self.model.appendRow(items)
                #self.model.layoutChanged.emit()

    def writeCsv(self, fileName):
        with open(fileName, "w") as fileOutput:                   
            writer = csv.writer(fileOutput, lineterminator='\n')  
            print('rowCount->', self.model.rowCount())
            for rowNumber in range(self.model.rowCount()):

                fields = [
                    self.model.data(
                        self.model.index(rowNumber, columnNumber),
                        Qt.DisplayRole
                    )
                    for columnNumber in range(self.model.columnCount())
                ]
                print('fields->', fields)
                writer.writerow(fields)

    @pyqtSlot()
    def on_pushButtonWrite_clicked(self):
        self.writeCsv(self.fileName)
        self.model.layoutChanged.emit()

    @pyqtSlot()
    def on_pushButtonLoad_clicked(self):
        self.loadCsv(self.fileName)

if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    app.setApplicationName('MyWindow')

    main = MyWindow("C:\Qt\csv.csv")
    main.show()

    sys.exit(app.exec_()) 

在上面的代码中,两个打印命令均得出True。我自然是在使用第一个。但是在我的讲义中,我看到了第二种方法。第一种检查状况的方法是否一直有效?

0 个答案:

没有答案