MemoryError: Unable to allocate 26.4 GiB for an array with shape (3540000000,) and data type float64

1. 读取 txt 文件

如果这样读取,

file = open('../data/entries_en.txt', 'r')
data = file.readlines()

会报内存不足

需要改成:

with open('../file.txt', 'r') as f:
    for line in f:
        # do stuff

这是逐行读取文件,而不是一次全部读取到 RAM

2. 读取 tdms 文件

查看 30 GB 的tdms 数据文件

file_keys = TdmsFile(file_dir).objects.keys()   # 查看keys
for key in file_keys:
    print(key)

报错

MemoryError: Unable to allocate 26.4 GiB for an array with shape (3540000000,) and data type float64

改为

with TdmsFile.open(file_dir) as tdms_file:
    file_keys = tdms_file.objects.keys()
    print(file_keys)

即可正常运行!!

你可能感兴趣的:(python)