Created
June 18, 2015 18:46
-
-
Save quocble/da0831343b98a1e9efae 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
# sum.py | |
# | |
# Sums up a specific column in a csv, input is via PIPE | |
# Useful for piping multiple files in at once. | |
import sys | |
import csv | |
if len(sys.argv) < 2: | |
print "Usage: cat file | python sum.py column#" | |
exit() | |
col = int(sys.argv[1]) | |
total = 0 | |
for row in csv.reader(iter(sys.stdin.readline, '')): | |
try: | |
total = total + float(row[col]) | |
print("value = " + row[col]) | |
except ValueError: | |
pass | |
sys.stdout.write("Total : " + str(total) + "\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment