title: Importing an irregular shaped array into Python
body: <p>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:</p>
  
  <pre><code>`
      {
      {-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}}}
      }
  `
  </code></pre>
  
  <p>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. </p>
  
  <p>I had some success with <code>numpy.genfromtxt("data.txt",delimiters="}}}")</code> which gives me the correct shape (126,2) but each element is simply "nan". </p>
  
  <p>I had some more success with</p>
  
  <pre><code>`
  with open("data.csv") as csvfile:
       reader = csv.reader(csvfile, delimiter=' ')
       for element in reader:
           print(np.asarray(element)[0])
  `
  </code></pre>
  
  <p>This gives me the integer values as an array, which is great! For the second position in each element i tried:</p>
  
  <pre><code>`
  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&lt;1:
               curlToSq=replace_all(str(element[1]),d)
               print(np.asarray(curlToSq))
  `
  </code></pre>
  
  <p>Where the <code>replace_all</code> 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 <em>does</em> return an array...of shape () with none of it's objects subscriptable, which is what I need!</p>
  
  <p>Any help is appreciated.</p>