Reading non-uniform data from file into array with NumPy Reading non-uniform data from file into array with NumPy numpy numpy

Reading non-uniform data from file into array with NumPy


Here's a one-liner:

arrays = [np.array(map(int, line.split())) for line in open('scienceVertices.txt')]

arrays is a list of numpy arrays.


for line in textfile:  a = np.array([int(v) for v in line.strip().split(" ")])  # Work on your array


You can also use numpy.fromstring()

for line in f:    a = numpy.fromstring(line.strip(), dtype=int, sep=" ")

or -- if you want full flexibility -- even numpy.loadtxt():

for line in f:    a = numpy.loadtxt(StringIO.StringIO(line), dtype=int)

For long lines, these solution will perform better than the Python code in the other answers.