Last active
August 29, 2015 14:06
-
-
Save janpipek/8b1131f567085bc7bd41 to your computer and use it in GitHub Desktop.
Load numpy array from a long text file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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