使用大型CSV文件时程序崩溃

时间:2018-10-19 13:46:28

标签: python pandas csv pycharm pyqt5

当我尝试使用大于3MB的文件时遇到问题。 我正在使用PyCharm来显示一个表,其中包含文件中包含的数据,代码如下:

import pandas as pd
import csv
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):
def setupUi(self, Dialog, fileName):
    Dialog.setObjectName("Dialog")
    Dialog.resize(500, 300)
    icon = QtGui.QIcon()      icon.addPixmap(QtGui.QPixmap("../../PycharmProjects/Stage/image/icon.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
    Dialog.setWindowIcon(icon)
    font = QtGui.QFont()
    font.setFamily("HP Simplified")
    Dialog.setFont(font)
    self.createTable(fileName)
    self.vBoxLayout = QtWidgets.QVBoxLayout()
    self.vBoxLayout.addWidget(self.tableWidget)
    Dialog.setLayout(self.vBoxLayout)
    self.retranslateUi(Dialog)
    QtCore.QMetaObject.connectSlotsByName(Dialog)
    # Funzione per la stampa della tabella nella finestra
def createTable(self, filename):
    fileCsv = pd.read_csv(filename)
    row_cont = fileCsv.shape[0]
    col_count = fileCsv.shape[1]
    self.tableWidget = QtWidgets.QTableWidget()
    self.tableWidget.setRowCount(row_cont+1)
    self.tableWidget.setColumnCount(col_count)
    with open(filename) as file:
        csvFile = csv.reader(file, delimiter=',')
        r = 0
        c = 0
        for row in csvFile:
            c = 0
            while c < len(row):
                self.tableWidget.setItem(r, c, QtWidgets.QTableWidgetItem(str(row[c])))
                c = c+1
            r = r+1

def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Dialog"))

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

“文件名”是我传递给该函数的csv文件的名称,因为它是由另一个类调用的。

我已经在文件idea.properties中编辑了两行: idea.max.intellisense.filesize = 2500到250000 和 idea.max.content.load.filesize = 20000转换为200000

编辑: 我已经将调试系统运行到发生错误的那一行: fileCsv = pd.read_csv(文件名)

控制台中的结果是:

C:\Python36\python.exe C:\PyCharm\helpers\pydev\pydevd.py --multiproc -- 
qt-support=auto --client 127.0.0.1 --port 65374 --file 

C:/Users/Enrico/PycharmProjects/Stage/PDA.py
pydev debugger: process 3780 is connecting

Connected to pydev debugger (build 182.4505.26)
Backend TkAgg is interactive backend. Turning interactive mode on.
sono qui
Traceback (most recent call last):
  File "pandas\_libs\parsers.pyx", line 1134, in pandas._libs.parsers.TextReader._convert_tokens
  File "pandas\_libs\parsers.pyx", line 1240, in pandas._libs.parsers.TextReader._convert_with_dtype
  File "pandas\_libs\parsers.pyx", line 1256, in pandas._libs.parsers.TextReader._string_convert
  File "pandas\_libs\parsers.pyx", line 1494, in pandas._libs.parsers._string_box_utf8
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 24: invalid continuation byte

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Enrico/PycharmProjects/Stage/PDA.py", line 149, in openFileCsv
    ui.setupUi(Dialog, self.lineEdit.text())
  File "C:\Users\Enrico\PycharmProjects\Stage\OpenCSV.py", line 15, in setupUi
    self.createTable(fileName)
  File "C:\Users\Enrico\PycharmProjects\Stage\OpenCSV.py", line 25, in createTable
    fileCsv = pd.read_csv(filename)
  File "C:\Python36\lib\site-packages\pandas\io\parsers.py", line 678, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Python36\lib\site-packages\pandas\io\parsers.py", line 446, in _read
    data = parser.read(nrows)
  File "C:\Python36\lib\site-packages\pandas\io\parsers.py", line 1036, in read
    ret = self._engine.read(nrows)
  File "C:\Python36\lib\site-packages\pandas\io\parsers.py", line 1848, in read
    data = self._reader.read(nrows)
  File "pandas\_libs\parsers.pyx", line 876, in pandas._libs.parsers.TextReader.read
  File "pandas\_libs\parsers.pyx", line 891, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas\_libs\parsers.pyx", line 968, in pandas._libs.parsers.TextReader._read_rows
  File "pandas\_libs\parsers.pyx", line 1094, in pandas._libs.parsers.TextReader._convert_column_data
  File "pandas\_libs\parsers.pyx", line 1141, in pandas._libs.parsers.TextReader._convert_tokens
  File "pandas\_libs\parsers.pyx", line 1240, in pandas._libs.parsers.TextReader._convert_with_dtype
  File "pandas\_libs\parsers.pyx", line 1256, in pandas._libs.parsers.TextReader._string_convert
  File "pandas\_libs\parsers.pyx", line 1494, in pandas._libs.parsers._string_box_utf8

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xeb in position 24: invalid continuation byte

0 个答案:

没有答案