1

I would like to write a number of Python Arrays into a txt file, with one array per line. After which I would like to read the Arrays line by line.

My work in progress code below. The problem I am working on involves about 100,000 arrays (length of L)

from __future__ import division
from array import array

M = array('I',[1,2,3])
N = array('I',[10,20,30])

L = [M,N]

with open('manyArrays.txt','w') as file:
    for a in L:
        sA = a.tostring()
        file.write(sA + '\n')

with open('manyArrays.txt','r') as file:
    for line in file:   
        lineRead = array('I', [])
        lineRead.fromstring(line)
        print MRead

The error message I get is

lineRead.fromstring(line)
ValueError: string length not a multiple of item size
1
  • array.array has a method called literally array.tostring. Commented Sep 14, 2018 at 16:19

1 Answer 1

1

You can either use numpy function for this, or code lines yourself:

You could concatenate your arrays in one 2D array and save it directly with np.savetxt, load it with np.genfromtext :

M = np.array([1,2,3],dtype='I')
N = np.array([10,20,30],dtype='I')
data= np.array([M,N])

file='test.txt'
np.savetxt(file,data)
M2,N2 = np.genfromtxt(file)

Or do :

file2='test2.txt'
form="%i %i %i \n"
with open(file2,'w') as f:
    for i in range(len(data)):
        f.write(form % (data[i,0],data[i,1],data[i,2]))
Sign up to request clarification or add additional context in comments.

2 Comments

array.array that OP use is not numpy.array.
Indeed @ForceBru. While this is not clear in my post, the numpy solution still works with array.array elements. np.savetxt('todel;txt',L) and M2,N2 = np.genfromtxt(file) will yield the wanted result. For the classical way you would just change the indexing with [i][k] : file2='test2.txt' form="%i %i %i \n" with open(file2,'w') as f: for i in range(len(data)): f.write(form % (data[i][0],data[i][1],data[i][2]))

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.