numpy.core._exceptions.MemoryError: Unable to allocate 512. KiB for an array with shape (65536,)


错误描述:
numpy.core._exceptions.MemoryError: Unable to allocate 512. KiB for an array with shape (65536,) and data type int64.

原因分析:
数据量太大,

Connected to pydev debugger (build 202.6397.98)
2023-03-29 16:35:22,484 - INFO - file_concat5001.py - main:157 - start concat…
start the file F:\02-data\11-测风塔(10hz)\5001_liuyuan\TOA5_4439.sec-100M-1.dat
0it [00:00, ?it/s]8 (‘TOA5’, ‘4439’, ‘CR1000X’, ‘4439’, ‘CR1000X.Std.06.00’, ‘CPU:3d-text-(ok)-1.CR1X’, ‘40973’, ‘sec-100M-1’)
63792268it [1:42:38, 10358.62it/s]

最主要的还是电脑内存不足,因为需要处理的数据量太大,GPU性能不够,存在内存溢出现象

读取超大文件

Python基于read(size)方法读取超大文件
主要介绍了Python基于read(size)方法读取超大文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下。

pyhon读取文件很方便,但是,如果文件很大,而且还是一行文件,那就蛋疼了. 不过还好有read(size)方法,这个方法就是每次读取size大小的数据到内存中,

下面来个示例

def readlines(f, separator):
	''' 读取大文件方法
	:param f: 文件句柄
	:param separator: 每一行的分隔符
	:return:
	'''
	buf = ''
	while True:
		while separator in buf:
			position = buf.index(separator) # 分隔符的位置
			yield buf[:position] # 切片, 从开始位置到分隔符位置
			buf = buf[position + len(separator):] # 再切片,将yield的数据切掉,保留剩下的数据
		chunk = f.read(4096) # 一次读取4096的数据到buf中
		if not chunk: # 如果没有读到数据
			yield buf # 返回buf中的数据
			break # 结束
		buf += chunk # 如果read有数据 ,将read到的数据加入到buf中
with open('text.txt', encoding='utf-8') as f:
	for line in readlines(f,'|||'):
	# 为什么readlines函数能够使用for循环遍历呢, 因为这个函数里面有yield关键字呀, 有它就是一个生成器函数 ......
	print(line)

测试文件text.txt
fgshfsljflsjfls|||fyhdiyfdfhn|||fudofdb钦铁杆jdlfdl|||tedsthfdskfdk
打印结果

fgshfsljflsjfls
fyhdiyfdfhn
fudofdb钦铁杆jdlfdl
tedsthfdskfdk

参考链接

[1] 解决numpy.core._exceptions.MemoryError: Unable to allocate 1.04 MiB for an array 2023.1;

猜你喜欢

转载自blog.csdn.net/weixin_46713695/article/details/129850574