Use
with open(...) as outfile:
#outfile.write()
#outfile.read()
#outfile.readlines()
#etc
to have the file automatically close for you. It looks better and you don't have to remember to close the file.
I just read the whole thing in and split the string into a list. Then I used the indices to create the array.
arr = np.array([lines[1:])
creates a flat array of each element in lines
starting at the 2nd element, so I resized it using the dim x dim
you provided with the resize
function.
import numpy as np
lines = []
with open('perc.txt', 'r') as outfile:
lines = outfile.read().split()
>>> print lines
>>> ['3', '1', '0', '1', '0', '1', '0', '1', '1', '1']
arr = np.array([lines[1:]])
dim = int(lines[0])
arr.resize(dim, dim)
>>> print arr
>>> [['1' '0' '1']
['0' '1' '0']
['1' '1' '1']]