8

I have a 3D Numpy array with the shape [1953,949,13]. I want to write it to a CSV file where each row should contain a 2D array of shape [949 13] and csv file should contain 1953 rows. I tried np.savetext and it supports only 1D and 2D arrays. Then I tried line by line writing to a CSV but it requires each matrix to be converted to a string. How can I get this done in python? My requirement is different from the question Storing values in a 3D array to csv

4
  • 3
    Possible duplicate of Python: Storing values in a 3D array to csv Commented May 22, 2018 at 2:57
  • 2
    How do you display a 3d structure as simple rows and columns? And read it back with code that expects that same csv layout? You can't without some sort of manipulation. Basically either reshape to and from 2d, or save the planes as separate 2d blocks. It's up to you to decide what the csv is supposed to look like. Commented May 22, 2018 at 3:01
  • @hpaulj got it. Thanks Commented May 22, 2018 at 3:06
  • @Sebastian I am expecting to write 2D matrices in each row of the CSV. I checked the answer to the question you pointed and the requirement is a bit different here. Commented May 22, 2018 at 3:11

2 Answers 2

8

I'm not sure if it's the best way to doing it, but I faced the same problem and here's how I solved it.

import csv
import numpy as np
fil_name = 'file'
example = np.zeros((2,3,4))
example = example.tolist()
with open(fil_name+'.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile, delimiter=',')
    writer.writerows(example)

#to read file you saved
with open(fil_name+'.csv', 'r') as f:
  reader = csv.reader(f)
  examples = list(reader)

print(examples)
nwexamples = []
for row in examples:
    nwrow = []
    for r in row:
        nwrow.append(eval(r))
    nwexamples.append(nwrow)
print(nwexamples)
Sign up to request clarification or add additional context in comments.

2 Comments

This solution is more general and works very well.
thank you for this. it worked for a 5D matrix!
4

I used this method instead, not aware of any better method:

# reshaping the array from 3D matrice to 2D matrice.
arrReshaped = arr.reshape(arr.shape[0], -1)
# saving reshaped array to file.
np.savetxt(filename, arrReshaped)
# retrieving data from file.
loadedArr = np.loadtxt(filename)
# This loadedArr is a 2D array, therefore we need to convert it to the original array shape.
# reshaping to get original matrice with original shape.
loadedOriginal = loadedArr.reshape(loadedArr.shape[0], loadedArr.shape[1] // arr.shape[2], arr.shape[2])

1 Comment

I think this solution is more efficient!

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.