Load text file as strings using numpy.loadtxt()

前端 未结 3 865
深忆病人
深忆病人 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:08

    There is also read_csv in Pandas, which is fast and supports non-comma column separators and automatic typing by column:

    import pandas as pd
    df = pd.read_csv('your_file',sep='\t')
    

    It can be converted to a NumPy array if you prefer that type with:

    import numpy as np
    arr = np.array(df)
    

    This is by far the easiest and most mature text import approach I've come across.

提交回复
热议问题