Load text file as strings using numpy.loadtxt()

前端 未结 3 866
深忆病人
深忆病人 2020-12-13 19:55

I would like to load a big text file (around 1 GB with 3*10^6 rows and 10 - 100 columns) as a 2D np-array containing strings. However, it seems like numpy.loadtxt() onl

3条回答
  •  误落风尘
    2020-12-13 20:31

    Use genfromtxt instead. It's a much more general method than loadtxt:

    import numpy as np
    print np.genfromtxt('col.txt',dtype='str')
    

    Using the file col.txt:

    foo bar
    cat dog
    man wine
    

    This gives:

    [['foo' 'bar']
     ['cat' 'dog']
     ['man' 'wine']]
    

    If you expect that each row has the same number of columns, read the first row and set the attribute filling_values to fix any missing rows.

提交回复
热议问题