csv.writer - TypeError:'str'不支持缓冲区接口

时间:2017-11-03 12:39:33

标签: python python-3.x csv

大家好我将导出获取DataBase sqlite但是当我想要导出时我得到以下错误显示

    def ExportFile(self):
    self.cursor.execute("SELECT * FROM Book")
    dbFile = QFileDialog.getSaveFileName(parent=None,caption="Export 
    DataFile a File",directory=".",filter="CSV File(*.csv)")

    if dbFile[0]:
        with open(dbFile[0], "wb") as csvFile:
            csvWriter = csv.writer(csvFile,delimiter=',', 
            quotechar="\"",quoting=csv.QUOTE_MINIMAL)

            rows = self.cursor.fetchall()
            rowCount = len(rows)

            for row in rows:
                csvWriter.writerow(row)

            QMessageBox.information(self,__appname__,"Succesfully Exported" 
            + str(rowCount) + " rows to a file\r\n" + 
            str(QDir.toNativeSeparators(dbFile[0])))

此错误:

Traceback (most recent call last):
File "D:/Souerce/PyQT/Project/Library Management/main.py", line 303, in 
ExportFile
csvWriter.writerow(row)
TypeError: 'str' does not support the buffer interface

1 个答案:

答案 0 :(得分:0)

       self.cursor.execute("SELECT * FROM Book")
       dbFile = QFileDialog.getSaveFileName(parent=None,caption="Export 
       DataFile a File",directory=".",filter="CSV File(*.csv)")

    if dbFile[0]:
        with open(dbFile[0] + ".csv", "wb") as csvFile:
            csvWriter = csv.writer(csvFile,delimiter=',', 
            quotechar="\"",quoting=csv.QUOTE_MINIMAL)

            rows = self.cursor.fetchall()
            rowCount = len(rows)

            for row in rows:
                csvWriter.writerow(bytes(row,"UTF-8"))

错误:

Traceback (most recent call last):
File "D:/Souerce/PyQT/Project/Library Management/main.py", line 303, in 
ExportFile

csvWriter.writerow(bytes(row,"UTF-8"))
TypeError: encoding or errors without a string argument