Convert a numpy array to a CSV string and a CSV string back to a numpy array

后端 未结 3 911
长情又很酷
长情又很酷 2020-12-30 07:55

I have to convert a numpy array of floats to a string (to store in a SQL DB) and then also convert the same string back into a numpy float array.

This is how I\'m go

3条回答
  •  时光取名叫无心
    2020-12-30 08:45

    First you should use join this way to avoid the last comma issue:

    VIstring = ','.join(['%.5f' % num for num in VI])
    

    Then to read it back, use numpy.fromstring:

    np.fromstring(VIstring, sep=',')
    

提交回复
热议问题