Convert a text file to a numpy array

后端 未结 1 1652
-上瘾入骨i
-上瘾入骨i 2021-01-20 04:07

I am trying to convert a list of strings into an array. The list is really an array of numbers that is n rows long by 4 columns that I took from a text file. I need to conve

相关标签:
1条回答
  • 2021-01-20 04:26

    Probably you just need the numpy.loadtxt function: http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html#numpy.loadtxt

    It worked for me on the input you posted:

    In [1]: import numpy as np
    
    In [2]: a = np.loadtxt('example.csv')
    
    In [3]: a
    Out[3]:
    array([[  3.12255300e-01,  -9.51537220e+00,   4.82394410e-09,
             -1.16142150e-08],
           [  4.02051220e-01,  -8.54049810e+00,   1.73968870e-09,
             -1.86658990e-08],
           [  4.32242510e-01,  -7.55654360e+00,   2.09856020e-09,
             -2.53499550e-08],
           [  4.32348700e-01,  -6.56939320e+00,   1.71662130e-09,
             -3.11563610e-08],
           [  4.22761930e-01,  -5.59055800e+00,   1.96270620e-09,
             -3.73170660e-08],
           [  4.02450470e-01,  -4.65858680e+00,   1.73055040e-09,
             -4.31531980e-08],
           [  3.62845620e-01,  -3.84946090e+00,   1.74221980e-09,
             -4.82496190e-08],
           [  3.12349370e-01,  -3.17677070e+00,   1.99018610e-09,
             -5.32210550e-08],
           [  2.67260670e-01,  -2.57439390e+00,   1.97994200e-09,
             -5.83436270e-08],
           [  2.27916160e-01,  -2.03802400e+00,   1.71501380e-09,
             -6.32505420e-08],
           [  1.82853480e-01,  -1.59975920e+00,   9.94285940e-10,
             -6.72492570e-08]])
    
    0 讨论(0)
提交回复
热议问题