1

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?

1 Answer 1

1

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

np.loadtxt("my_file.txt", delimiter=",")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.