Skip to content

Instantly share code, notes, and snippets.

@quocble
Created June 18, 2015 18:46
Show Gist options
  • Save quocble/da0831343b98a1e9efae to your computer and use it in GitHub Desktop.
Save quocble/da0831343b98a1e9efae to your computer and use it in GitHub Desktop.
# 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