1

I want to save a 3 dimensional arrays values to a txt or csv file in python. dCx, dCy

I used:

numpy.savetxt('C:/Users/musa/Desktop/LOCO_All_tests/FODO_Example/AllQ/dCx.csv',dCx,delimiter=',')
numpy.savetxt('C:/Users/musa/Desktop/LOCO_All_tests/FODO_Example/AllQ/dCy.csv',dCy,delimiter=',')

And to load it again:

dCx = numpy.genfromtxt('C:/Users/musa/Desktop/LOCO_All_tests/FODO_Example/AllQ/dCx.csv', delimiter=',')
dCy = numpy.genfromtxt('C:/Users/musa/Desktop/LOCO_All_tests/FODO_Example/AllQ/dCy.csv', delimiter=',')

But i got the error massage

"Expected 1D or 2D array, got 3D array instead"

Si i wanted to change the 3d arrays first to 2 arrays and then save it to the files, and when uploaded again i convert it back to 3d for example:

dCx2 = np.array(dCx).reshape(np.array(dCx).shape[0], -1)
dCy2 = np.array(dCy).reshape(np.array(dCy).shape[0], -1)

and after loaded to variable named dCx3 and dCy3 i used:

dCx = np.array(dCx3).reshape(
    np.array(dCx3).shape[0], np.array(dCx3).shape[1] // np.array(dCx).shape[2], np.array(dCx).shape[2])

#dCy = np.array(dCy3).reshape(
#    np.array(dCy3).shape[0], np.array(dCy3).shape[1] // np.array(dCy).shape[2], np.array(dCy).shape[2])

I am looking for a better method that i can used in the saving the 3d arrays to file, or a method to convert the 2d into 3d without having to measure the original arrays every time as it is used in this line:

 np.array(dCy).shape[2], np.array(dCy).shape[2])
4
  • The CSV format is inherently tabular format; it is not suitable for three dimensional data. Commented Aug 30, 2022 at 14:43
  • Are you doing anything with the files in between saving them to disk, and reading them back into memory/NumPy again? Commented Aug 30, 2022 at 14:43
  • @9769953 No, the files are not touched Commented Aug 30, 2022 at 14:48
  • 1
    Why not use numpy.save(filepath, data) and data = numpy.load(filepath)? These are binary file formats, and generic for any type of NumPy data. Commented Aug 30, 2022 at 14:56

2 Answers 2

2

Use numpy.save(filepath, data) and data = numpy.load(filepath).

These are binary file formats, and generic for any type of NumPy data

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

Comments

0

Try tofile. it works for in my case. but array will write in 1D

import numpy as np
arr=np.arange(0,21).reshape(7,3)
arr.tofile('file.txt',sep=',')
arr2=np.fromfile('file.txt',sep=',')

2 Comments

it works but as you mentioned the arrays will return in 1 D so still the same problem
in that case iterate over every row, and append to the file.\n import numpy as np arr=np.arange(0,21).reshape(7,3) print(arr) for i in arr: with open('file.txt','a') as fp: fp.write(str(i)) fp.write("\n")

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.