Skip to content

Instantly share code, notes, and snippets.

@vr2262
Created February 4, 2013 11:32
Show Gist options
  • Save vr2262/4706239 to your computer and use it in GitHub Desktop.
Save vr2262/4706239 to your computer and use it in GitHub Desktop.
import numpy as np
import csv
def compute_sum(infile):
"""
Returns the sum of numbers in a file with one column and a header row.
Parameters
----------
infile : File object
Should be opened with 'r' permissions
Notes
-----
Treats values in the column as floating point numbers
"""
# Try using the csv module
reader = csv.reader(infile)
try:
float(reader.next())
reader.seek(0)
except TypeError:
pass
return sum(float(datum[0]) for datum in reader)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment