Why does python think my array is 0-d? (TypeError: iteration over a 0-d array)
<p>I'm aware that this may have been answered before but <strong>please check that other answers are relevant to this instance!</strong></p>

<p>I'm writing an array to a file as a string so that it can be stored when my script isn't running and be accessed again easily when run. When I then read this string out of the file again it is automatically read as a string.</p>

<p>I can fix this with a for loop that iterates through the saved string and appends each entry to an empty array, but that seems like overkill - is it? Is there a better way to read a string and convert it to an array?</p>

<p>So... something like this runs the first time in order to generate an array and write it to a file as a string:</p>

<pre><code>the_file = open('.the_file.txt', 'w')
the_array = [10, 20, 30, 40, 50]
the_file.write(str(the_array))
the_file.close()
</code></pre>

<p>The next time the code is run a different section of code gets run that is something like this:</p>

<pre><code>the_file = open('.the_file.txt', 'r')
the_array = the_file.read()
the_file.close()
</code></pre>

<p>If I print the the_array variable and its type at this point I get:</p>

<pre><code>[10, 20, 30, 40, 50]
&lt;type 'str'&gt;
</code></pre>

<p>So I follow the advice given in <a href="https://stackoverflow.com/questions/18560993/how-to-index-0-d-array-in-python">this similar question here</a> and do:</p>

<pre><code>the_array = np.asarray(the_array)
</code></pre>

<p>...and then print the the_array variable and its type at this point to check that this has worked, to get:</p>

<pre><code>[10, 20, 30, 40, 50]
&lt;type 'numpy.ndarray'&gt;
</code></pre>

<p>But now when I run the rest of my code I get the error:</p>

<pre><code>TypeError: iteration over a 0-d array
</code></pre>

<p>Traced back to this part of my code:</p>

<pre><code>the_array = sorted(the_array, reverse=True)
</code></pre>

<p>Can anyone help me out here; why does python think that my array is 0-d?</p>