Created
January 18, 2013 10:35
-
-
Save seumasmorrison/4563724 to your computer and use it in GitHub Desktop.
Fast and memory efficient solution for loading well formed large csv files
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
# Code copied from Joe Kington's StackOverflow answer | |
# http://stackoverflow.com/a/8964779/1135883 | |
# For large files with regular records this is a much faster and memory | |
# efficient solution | |
def iter_loadtxt(filename, delimiter=' ', skiprows=0, dtype=float): | |
def iter_func(): | |
with open(filename, 'r') as infile: | |
for _ in range(skiprows): | |
next(infile) | |
for line in infile: | |
line = line.rstrip().split(delimiter) | |
for item in line: | |
yield dtype(item) | |
iter_loadtxt.rowlength = len(line) | |
data = np.fromiter(iter_func(), dtype=dtype) | |
data = data.reshape((-1, iter_loadtxt.rowlength)) | |
return data | |
#generate_text_file() | |
data = iter_loadtxt('Area3_final 0_5m.XYZ') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment