Reading Non-uniform Data From File Into Array With Numpy
Suppose I have a text file that looks like this: 33 3 46 12 23 10 23 11 23 12 23 13 23 14 23 15 23 16 24 10 24 11 24 12 24 13 24 14 24 15 24 16 25 14 25 15 25 16 26 16 27 1
Solution 1:
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.
Solution 2:
for line in textfile:
a = np.array([int(v) for v in line.strip().split(" ")])
# Work on your array
Solution 3:
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.
Solution 4:
f = open("file", "r")
array = []
line = f.readline()
index = 0while line:
line = line.strip("\n")
line = line.split()
array.append([])
for item in line:
array[index].append(int(item))
line = f.readline()
index += 1
f.close()
print array
Post a Comment for "Reading Non-uniform Data From File Into Array With Numpy"