0

I've seen how to save a numpy array with a format option (here). I saved my numpy array like this:

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

It looks fine:

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

Now when I try to load it I get an error:

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

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

I tried this, to math the fmt option:

loaded_file = np.loadtxt("my_file.txt", dtype='f')

but got the same error.

How can I load my numpy array?

Community
  • 1
  • 1
StatsSorceress
  • 3,019
  • 7
  • 41
  • 82

1 Answers1

1

The default delimiter value is a whitespace. You have to provide the value of the actual delimiter:

np.loadtxt("my_file.txt", delimiter=",")
DYZ
  • 55,249
  • 10
  • 64
  • 93