How to get a numpy ndarray of integers from a file with header?
<p>I have a plain text file (.txt) with the following content.</p>

<pre><code>Matrix Header.
6 11
0 1 1 1 1 1 1 1 1 1 1 
1 0 1 1 1 1 0 1 1 1 1 
1 1 1 1 0 0 1 1 1 1 1 
0 0 0 0 1 1 1 0 0 0 0 
1 1 1 0 0 1 1 1 1 1 1 
1 0 0 1 1 1 1 0 1 1 0 

 6 rows,  11 columns
</code></pre>

<p>I need obtain a numpy ndarray of integers as below:</p>

<pre><code>[[0 1 1 1 1 1 1 1 1 1 1] 
 [1 0 1 1 1 1 0 1 1 1 1] 
 [1 1 1 1 0 0 1 1 1 1 1] 
 [0 0 0 0 1 1 1 0 0 0 0] 
 [1 1 1 0 0 1 1 1 1 1 1] 
 [1 0 0 1 1 1 1 0 1 1 0]]
</code></pre>

<p>I tried the following strategy</p>

<pre><code>import pandas
import numpy
data = pandas.read_table(path, skiprows= 2)
data = data.values
print(data)
</code></pre>

<p>But the resulting ndarray isn't in the correct format.</p>

<pre><code>[['0 1 1 1 1 1 1 1 1 1 1 '] 
 ['1 0 1 1 1 1 0 1 1 1 1 '] 
 ['1 1 1 1 0 0 1 1 1 1 1 '] 
 ['0 0 0 0 1 1 1 0 0 0 0 '] 
 ['1 1 1 0 0 1 1 1 1 1 1 '] 
 ['1 0 0 1 1 1 1 0 1 1 0 ']]
</code></pre>

<p>Can anybody help me?</p>

<p>A simple solution is to explicitly ignore the lines you don't need:</p>

<pre><code>with open(path) as infile:
    lines = infile.readlines()
np.loadtxt(lines[2:-2])
del lines # if you want to immediately release the memory
</code></pre>

<p>This directly gives you what you want, assuming the header and footer are always two lines each.</p>

python numpy multidimensional-array