Skip to content

Instantly share code, notes, and snippets.

@lambdamusic

lambdamusic/inspectCSV.py

Last active Nov 30, 2020
Embed
What would you like to do?
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.
#!/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