如何在numpy中添加进度指示器,loadtxt?

ruarlubt  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(123)

我需要使用numpy loadtxt将非常大的文本CSV文件加载到RAM中。是否有一种方法可以添加进度指示器来显示正在读取文件?我的代码看起来像这样:

import numpy as np
data = np.loadtxt(filename, dtype=np.int64)
brgchamk

brgchamk1#

我通过使用向loadtxt添加自定义转换器的功能解决了这个问题。打印a。来安慰每一百万件物品。

def conv(x:str):
    if b'.' in x: # if the number has a . in it
        v= int(1e6*float(x)) # treat column zero as floating point seconds and convert to int us
    else:
        v= int(x) # otherwise convert the str to int
    conv.event_counter+=1
    if conv.event_counter%1000000==0:
        print('.',end='')
    return v
conv.event_counter=0 # define function static variable after function, put it in function
events = np.loadtxt(events_path, dtype=np.int64,converters=conv)

一个小实验表明,这种函数方法比使用lambda函数的dict快得多,每个列一个函数。

相关问题