Home > Mobile >  List of arrays into .txt file without brackets and well spaced
List of arrays into .txt file without brackets and well spaced

Time:01-20

I'm trying to save .txt file of a list of arrays as follow :

list_array =
    
[array([-20.10400009,  -9.94099998, -27.10300064]),
 array([-20.42099953,  -9.91499996, -27.07099915]),
 
 ...
 

This is the line I invoked.

np.savetxt('path/file.txt', list_array, fmt='%s')

This is what I get

[-20.10400009  -9.94099998 -27.10300064]
[-20.42099953  -9.91499996 -27.07099915]
 
 ...

This is what I want

-20.10400009 -9.94099998 -27.10300064
-20.42099953 -9.91499996 -27.07099915
 
 ...

EDIT :

It is translated from Matlab as followed where I .append to transform

Cell([array([[[-20.10400009,  -9.94099998, -27.10300064]]]),
       array([[[-20.42099953,  -9.91499996, -27.07099915]]]),
       array([[[-20.11199951,  -9.88199997, -27.16399956]]]),
       array([[[-19.99500084, -10.0539999 , -27.13899994]]]),
       array([[[-20.4109993 ,  -9.87100029, -27.12800026]]])],
      dtype=object)

CodePudding user response:

I cannot really see what is wrong with your code, except for the missing imports. With array, do you mean numpy.array, or are you importing like from numpy import array (which you should refrain from doing)?

Running this example gives exactly what you want.

import numpy as np

list_array = [np.array([-20.10400009,  -9.94099998, -27.10300064]),
 np.array([-20.42099953,  -9.91499996, -27.07099915])]

np.savetxt('test.txt', list_array, fmt='%s')
> cat test.txt
-20.10400009 -9.94099998 -27.10300064
-20.42099953 -9.91499996 -27.07099915

CodePudding user response:

I tried to reproduce your example, but I got the same thing as @Lukas. I as well see no problem with your code.

import numpy as np

list_array = [np.array([-20.10400009,  -9.94099998, -27.10300064]),
 np.array([-20.42099953,  -9.91499996, -27.07099915])]

np.savetxt('test.txt', list_array, fmt='%s')

Gives me this:

> cat .\test.txt
-20.10400009 -9.94099998 -27.10300064
-20.42099953 -9.91499996 -27.07099915

This works fine as well:

import numpy as np

list_array = [([-20.10400009,  -9.94099998, -27.10300064]),
 ([-20.42099953,  -9.91499996, -27.07099915])]

np.savetxt('test.txt', list_array, fmt='%s')
  •  Tags:  
  • Related