Skip a specified number of columns with numpy.genfromtxt()

前端 未结 2 1932
没有蜡笔的小新
没有蜡笔的小新 2021-02-06 10:16

I have a large table (numbers in text format) that I would like to load with numpy.genfromtxt(). I would like to ignore the first n columns, say 5. I do no

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-06 10:55

    For older versions of numpy, peeking at the first line to discover the number of columns is not that hard:

    import numpy as np
    with open(fname, 'r') as f:
        num_cols = len(f.readline().split())
        f.seek(0)
        data = np.genfromtxt(f, usecols = range(5,num_cols))
    print(data)
    

提交回复
热议问题