zazizoma
zazizoma

Reputation: 567

numpy.savetxt for 2d array in Python 3.5.1

my question is related to this but I can't get that solution to work and didn't want to add my own scenario to the old question.

I have a 2D float numpy array, am running python 3.5.1 with numpy 1.10.4, and am trying to write out the array with

numpy.savetext(filename, arrayname, delimiter = ',') 

which works beautifully with a 1D array. I've tried the solution from the referenced post

with open(filename, 'ab') as f:
    numpy.savetext(f, arrayname, delimiter = ',')

to no avail. Actually, I've tried this without the delimiter as well as with 'w', 'wb, 'a' and with formatting arguments, and always get the same error message:

TypeError: Mismatch between array dtype ('float64') and format specifier.

I need to write this 2D array to a file which will be read later into a panda dataframe (have been using read.csv). I understand this may be an issue with numpy.savetxt, so I'm looking for an alternative.

Upvotes: 1

Views: 6099

Answers (2)

Anonymous
Anonymous

Reputation: 11

I had the same error message - until I finally realized that the type of my output actually was a list, not a numpy array!

Upvotes: 1

roadrunner66
roadrunner66

Reputation: 7941

Please try a minimal example and post the result, since the following works for me:

import numpy as np

array1=np.array([[1,2],[3,4]])
np.savetxt('file1.txt', array1 , delimiter = ',') 

file content:

 1.000000000000000000e+00,2.000000000000000000e+00
 3.000000000000000000e+00,4.000000000000000000e+00

Upvotes: 2

Related Questions