1

My array of Integers gets saved as an array of Floats.

array = [1, 2, 3, 4, 5, 6, 7];
print(array)
>> [1, 2, 3, 4, 5, 6, 7]

np.savetxt('labels.txt', array, delimiter=",");

test = np.loadtxt("labels.txt", delimiter=",")
print(test)
>> [1. 2. 3. 4. 5. 6. 7.]

My created labels.txt looks like this:

1.000000000000000000e+00 2.000000000000000000e+00 3.000000000000000000e+00 4.000000000000000000e+00 5.000000000000000000e+00 6.000000000000000000e+00 7.000000000000000000e+00

But I expect the text to be 1, 2, 3, 4, 5, 6, 7. How can I achieve this?

4
  • 4
    The default save format is: fmt='%.18e'. The savetxt docs discusses altenative fmt values. Commented Jan 3, 2020 at 20:56
  • You can control the format that you save the array using fmt of savetxt, then you can also specify the dtype during loadtxt when you read back in. Commented Jan 3, 2020 at 20:57
  • 1
    float is the default dtype for loadtxt as well. Commented Jan 3, 2020 at 20:58
  • @prune, I am removing the duplicate. That SO is about assigning elements in a np.empty array. This is about writing values to a csv. Commented Jan 3, 2020 at 23:28

2 Answers 2

2

Use the fmt input of %i (integer formatting) to np.savetxt

import numpy as np

testList = [1, 2, 3, 4, 5]
np.savetxt("testList.txt", [testList], fmt="%i", delimiter=",", newline="\n")

This gives the output 1,2,3,4,5. Note that the input to savetxt is a list of lists. That is because the delimiter separates column values and the newline separates rows (so you need 2D to get the delimiters).

Sign up to request clarification or add additional context in comments.

Comments

1

If you don't have anything confining you to np.savetext, then try using pickle:

import numpy as np
import pickle as pkl

path = r"path/to/dump.pkl"
a = np.array([1,2,3,4,5,6,7])
b = pkl.dump(a, path)
c = pkl.load(path)
np.testing.assertequal(c, a)

Or in console if you feel more like it,

>>> import numpy as np
>>> a = [1, 2, 3, 4, 5, 6, 7]
>>> a
[1, 2, 3, 4, 5, 6, 7]
>>> b = np.array(a)
>>> b
array([1, 2, 3, 4, 5, 6, 7])
>>> import pickle
>>> pickle.dumps(b)
b'\x80\x03cnumpy.core.multiarray\n_reconstruct\nq\x00cnumpy\nndarray\nq\x01K\x00\x85q\x02C\x01bq\x03\x87q\x04Rq\x05(K\x01K\x07\x85q\x06cnumpy\ndtype\nq\x07X\x02\x00\x00\x00i4q\x08K\x00K\x01\x87q\tRq\n(K\x03X\x01\x00\x00\x00<q\x0bNNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tq\x0cb\x89C\x1c\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00q\rtq\x0eb.'
>>> p = pickle.dumps(b)
>>> d = pickle.loads(p)
>>> d
array([1, 2, 3, 4, 5, 6, 7])

This won't be a human readable file, but it answers your requirement.


or, just

with open(path, 'wb') as f:
    np.savetxt(f, a, fmt='%i', delimiter=",")

Which I personally favor less than the standard pickle

2 Comments

I'm not required to use np but I need it to be a .txt file
@Jonas Also added the csv option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.