I am trying to read multiple arrays saved in .txt file. I present the data in Test.txt file as well as the current and expected outputs.
import re
import numpy as np
import ast
with open('Test.txt') as f:
s = f.readlines()
#print(s)
s = ' '.join(s)
s = re.findall("\((\[[\w\W]*\])\)",s)
s=ast.literal_eval(s[0])
s=np.array(s)
print([s])
The data in Test.txt is
[array([[1.7],
[2.8],
[3.9],
[5.2]])]
[array([[2.1],
[8.7],
[6.9],
[4.9]])]
The current output is
line 4
[5.2]])]
^
SyntaxError: unmatched ')'
The expected output is
[array([[1.7],
[2.8],
[3.9],
[5.2]])]
[array([[2.1],
[8.7],
[6.9],
[4.9]])]
eval(' '.join(f.readlines()).replace('array', 'np.array').replace(')]', ')],'))should do the job while not being very robust. Why not using npy files or hdf5 for that which are specifically design for such use case and avoid such AST/eval ugly inefficient tricks?([array([[1.7], [2.8], [3.9], [5.2]])],)which is not the expected output.replace('\n', '')was missing. Still, it create a big array and not many one becausenp.arraycannot create the expected result in one call. You need to parse each array separately and callnp.arrayon each and then add each array to a list withappend. Regarding the output, it looks like you want an array of shape (n, 1) containing object arrays of shape (m,1). Object-based array are discouraged as they are inefficiently supported (and are not really useful compared to lists here). Please clarify the output type.