Skip to content

Instantly share code, notes, and snippets.

@janpipek
Last active August 29, 2015 14:06
Show Gist options
  • Save janpipek/8b1131f567085bc7bd41 to your computer and use it in GitHub Desktop.
Save janpipek/8b1131f567085bc7bd41 to your computer and use it in GitHub Desktop.
Load numpy array from a long text file
def my_loadtxt(fname, columns=-1, delimiter=',', dtype='float32'):
with open(fname) as f:
if columns == -1:
line = f.readline()
columns = np.fromstring(line, sep=delimiter).shape[0]
f.seek(0)
rows = 0
for line in f:
rows += 1
f.seek(0)
array = np.ndarray((rows, columns), dtype=dtype)
for i, line in enumerate(f):
array[i] = np.fromstring(line, sep=delimiter)[:columns]
return array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment