使用带有格式选项的savetxt加载已保存的numpy数组

时间:2018-03-24 22:17:13

标签: python numpy

我已经看过如何使用格式选项(here)保存numpy数组。我保存了我的numpy数组:

FROM python:3

RUN easy_install pip
RUN pip install django==1.9.12
RUN pip install requests

ADD . /.
WORKDIR /django.testsite

CMD ["python", "manage.py", "runserver", "5000"]

看起来很好:

np.savetxt('my_file.txt', results, fmt='%1.4f', delimiter=",")

现在当我尝试加载它时出现错误:

pl_sioux = np.loadtxt(" my_file.txt")

0.0050,0.0100,0.0150,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0100,0.0150,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0100,0.0200,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0100,0.0150,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0100,0.0100,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0050,0.0100,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0100,0.0150,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0100,0.0100,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0100,0.0100,0.0000,0.0000,0.0050,0.0000,0.0150
0.0050,0.0100,0.0100,0.0000,0.0000,0.0050,0.0000,0.0150

我试过这个,用数学Traceback (most recent call last): File "rfresults.py", line 3, in <module> pl_sioux = np.loadtxt("rf_pl_Sioux.txt") #, dtype='f') File "/user/pkgs/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 1092, in loadtxt for x in read_data(_loadtxt_chunksize): File "/user/pkgs/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 1019, in read_data items = [conv(val) for (conv, val) in zip(converters, vals)] File "/user/pkgs/anaconda2/lib/python2.7/site-packages/numpy/lib/npyio.py", line 738, in floatconv return float(x) ValueError: invalid literal for float(): 0.0050,0.0100,0.0150,0.0000,0.0000,0.0050,0.0000,0.0150 选项:

fmt

但得到了同样的错误。

如何加载我的numpy数组?

1 个答案:

答案 0 :(得分:1)

默认分隔符值是一个空格。您必须提供实际分隔符的值:

np.loadtxt("my_file.txt", delimiter=",")