Created
February 4, 2013 11:32
-
-
Save vr2262/4706239 to your computer and use it in GitHub Desktop.
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
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