Skip to content

Instantly share code, notes, and snippets.

@seumasmorrison
Created January 18, 2013 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seumasmorrison/4563724 to your computer and use it in GitHub Desktop.
Save seumasmorrison/4563724 to your computer and use it in GitHub Desktop.
Fast and memory efficient solution for loading well formed large csv files
# 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