Last active
November 30, 2020 17:20
-
-
Save lambdamusic/6353892 to your computer and use it in GitHub Desktop.
Script that reads a csv file and serializes it into a list of dictionaries {column: value}
The dicts can then be used for counting or to create other stats.
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
""" | |
csvreader.py | |
2013-08-27 | |
Script that reads a csv file and serializes it into a list of dictionaries {column: value} | |
The dicts can then be used for counting or to create other stats. | |
Note: spaces in file path should be represented simply as spaces (no escapes) | |
How: substitute the path of **filename** and run the script. | |
""" | |
import csv | |
filename = "test.csv" | |
f = open(filename, 'rU') # rU: http://www.gossamer-threads.com/lists/python/dev/723649 | |
bag = [] | |
try: | |
reader = csv.DictReader(f) | |
for row in reader: | |
bag.append(row) | |
finally: | |
f.close() | |
print "There are %d statements" % len(bag) | |
keys = bag[0].keys() | |
print "There are %d keys: %s" % (len(keys), ", ".join(["|"+x+"|" for x in keys])) | |
def getTotUniqueEl(bag, key): | |
exit = [] | |
for x in bag: | |
if x[key] not in exit: | |
exit += [x[key]] | |
return sorted(exit) | |
for x in keys: | |
els = getTotUniqueEl(bag, x) | |
print "*" * 10 | |
print "The key |%s| has %d unique elements" % (x, len(els)) | |
print els | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment