我正在使用熊猫read_csv
3.8 Gig文本文件,该文件以竖线分隔,但是将文件读入内存时出错。
这是我的read_in_files()
函数抛出的全部错误:
Reading in file C:\Users\cdabel\Desktop\_Temp\Master_Extract_Data_Mart_201909240935.txt
Traceback (most recent call last):
File "<stdin>", line 10, in <module>
File "<stdin>", line 7, in read_in_files
File "c:\python36\lib\site-packages\pandas\io\parsers.py", line 685, in parser_f
return _read(filepath_or_buffer, kwds)
File "c:\python36\lib\site-packages\pandas\io\parsers.py", line 463, in _read
data = parser.read(nrows)
File "c:\python36\lib\site-packages\pandas\io\parsers.py", line 1154, in read
ret = self._engine.read(nrows)
File "c:\python36\lib\site-packages\pandas\io\parsers.py", line 2048, in read
data = self._reader.read(nrows)
File "pandas\_libs\parsers.pyx", line 879, in pandas._libs.parsers.TextReader.read
File "pandas\_libs\parsers.pyx", line 894, in pandas._libs.parsers.TextReader._read_low_memory
File "pandas\_libs\parsers.pyx", line 948, in pandas._libs.parsers.TextReader._read_rows
File "pandas\_libs\parsers.pyx", line 935, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas\_libs\parsers.pyx", line 2130, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Unknown error in IO callback
此错误的可能原因是什么?可能与记忆有关吗?我该如何解决?我应该对这些数据进行分块吗?
我不怀疑RAM问题,因为在调用函数时我有7 GB以上的未使用RAM,如Windows 10 Task Manager性能监视器中所示。另外,我无法提供基础数据的任何示例,因为它是运行状况和PII数据。
import os
import pandas as pd
# File
filepath = "C:\\Temp\\datafile.txt"
filename_w_ext = "datafile.txt"
# Read in TXT file
def read_in_files(filepath, filename_w_ext):
filename, file_ext = os.path.splitext(filename_w_ext)
print('Reading in file {}'.format(filepath))
with open(filepath, "r", newline='') as file:
global df_data
# Here's where it errors:
df_data = pd.read_csv(file, dtype=str, sep='|')
return df_data.columns.values.tolist(), df_data.values.tolist()
搜索此特定错误仅提供了熊猫Tokenizer code
中错误处理的源代码。static int parser_buffer_bytes(parser_t *self, size_t nbytes) {
int status;
size_t bytes_read;
status = 0;
self->datapos = 0;
self->data = self->cb_io(self->source, nbytes, &bytes_read, &status);
TRACE((
"parser_buffer_bytes self->cb_io: nbytes=%zu, datalen: %d, status=%d\n",
nbytes, bytes_read, status));
self->datalen = bytes_read;
if (status != REACHED_EOF && self->data == NULL) {
int64_t bufsize = 200;
self->error_msg = (char *)malloc(bufsize);
if (status == CALLING_READ_FAILED) {
snprintf(self->error_msg, bufsize,
"Calling read(nbytes) on source failed. "
"Try engine='python'.");
} else {
snprintf(self->error_msg, bufsize, "Unknown error in IO callback");
}
return -1;
}
TRACE(("datalen: %d\n", self->datalen));
return status;
}
答案 0 :(得分:0)
在功能更强大的服务器上进行测试后,我现在意识到此错误显然是由于该文件需要114列的4 GB文件需要25至35 GB的可用RAM。这实际上应该引发内存不足的错误,但是我认为RAM中增加的数量超过了Tokenizer代码检查内存即将耗尽的能力。