I have some data generated in Mathematica that I need imported into Python. The way the data is generated relies on symbolic calculations so simply generating it in Python is out of the question. The data is an array of dimensions (126,2) but, where the first position in each element is an integer, the second position is a list of lists and the dimensions are not constant from element to element, for example:
`
{
{-9,{{4,2},{5,6},{8,10}}},
{-2,{{3,6},{6,1}}}
{4,{{3,6},{6,1},{3,6},{6,1},{3,6},{6,1},{3,6},{6,1}}}
}
`
would be the first three elements. The second position in each element will always a list of 2-D lists. The goal here is to have this data imported as a numpy array such that I can call each element, no matter it's position.
I had some success with numpy.genfromtxt("data.txt",delimiters="}}}") which gives me the correct shape (126,2) but each element is simply "nan".
I had some more success with
`
with open("data.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=' ')
for element in reader:
print(np.asarray(element)[0])
`
This gives me the integer values as an array, which is great! For the second position in each element i tried:
`
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
d={"{":"[","}":"]"}
with open("spinweights.csv") as csvfile:
reader = csv.reader(csvfile, delimiter=',')
it=0
for element in reader:
while it<1:
curlToSq=replace_all(str(element[1]),d)
print(np.asarray(curlToSq))
`
Where the replace_all function is changing all curly brackets in square brackets (the thinking here was this would make it easier to convert into a numpy array). The final line there does return an array...of shape () with none of it's objects subscriptable, which is what I need!
Any help is appreciated.