0

I'm having a problem trying to save an array of arrays as csv using numpy.

The array of arrays:

a = numpy.asarray(output)
print(a)

it outputs (i kept only one item in the array, for the example):

[
['0.014897877' '-0.08609378' '-0.17572552' '0.14012611' '-0.034625955'
  '-0.008969465' '-0.020985316' '-0.13464274' '-0.0643522' '0.001874223'
  '0.093106195' '0.01346092' '0.17929164' '-0.019824918' '0.125187'
  '-0.030954123' '0.06412735' '-0.025960516' '-0.14795333' '0.0026848102'
  '-0.15260652' '0.033525705' '0.03411285' '-0.1506365' '-0.028831841'
  '-0.07956695' '0.15328659' '0.0019746106' '0.0031366237' '0.07046314'
  '0.052344024' '0.05874188' '0.09005664' '0.068730354' '-0.08379446'
  '0.012004613' '0.10616668' '0.03131739' '-0.07437438' '-0.1299052'
  '0.15480998' '0.11262169' '0.032479584' '-0.08733604' '-0.016337194'
  '-0.17954016' '0.086337775' '-0.06776995' '0.10646294' '0.10496249'
  '0.004988468' '-0.11673802' '-0.0628141' '0.096142575' '0.03181175'
  '0.008554184' '-0.123010606' '0.0027755483' '-0.04792862' '-0.11383578'
  '-0.0071639013' '-0.012682551' '0.04330155' '0.13239346' '0.06173887'
  '0.04698543' '-0.10461798' '0.10343763' '-0.14041597' '0.04108579'
  '-0.0041574505' '0.06904513' '0.06497475' '-0.054304164' '0.11304527'
  '0.016850471' '0.008820267' '-0.056193784' '-0.021828642'
  '-0.016804473' '-0.15866709' '0.14507978' '-0.033435807' '0.1639024'
  '0.069541104' '0.01782177' '0.0115199955' '0.016909525' '-0.050565705'
  '-0.16228318' '-0.028010793' '0.01789277' '0.09902625' '-0.00567781'
  '0.09101357' '-0.01005199' '-0.01796569' '0.13880818' '-0.059297908'
  '-0.120813474' '0.05444644' '0.037819214' '-0.029543832' '0.0038782186'
  '0.13723414' '0.17920697' '0.036572605' '-0.06763435' '-0.01860031'
  '-0.021825034' '-0.025996473' '0.1434528' '-0.08229978'
  '-0.00014224448' '-0.058567036' '0.023525652' '-0.18324575'
  '-0.11447681' '-0.063667014' '0.07024462' '-0.010663624' '-0.043397065'
  '0.24583343' '0.0026523445' '0.006540918' '-0.005765302' '0.008451278'
  '0.2030176' '5ca5204047427b2e5b29a5e2']
]

When I try to save using these format (128 float columns and the last as string)

numpy.savetxt("foo.csv", a, delimiter=",", fmt=''.join(['%1.20f,']*128) + '%s')

the console outputs:

numpy.savetxt("foo.csv", a, delimiter=",", fmt=''.join(['%1.20f,']*128) + '%s')
TypeError: Mismatch between array dtype ('<U32') and format specifier ('%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%1.20f,%s')

Any help?

Christian Benseler
  • 7,907
  • 8
  • 40
  • 71
  • What is the shape of `a`? I doubt it is `(1,129)`... – sentence Apr 22 '19 at 20:59
  • Possible duplicate of [python numpy.savetext a matrix with mixed format](https://stackoverflow.com/questions/49670586/python-numpy-savetext-a-matrix-with-mixed-format) – Ricky Kim Apr 22 '19 at 21:11

0 Answers0