Skip to content

Instantly share code, notes, and snippets.

@theotherzach
Last active January 14, 2017 20:44
Show Gist options
  • Save theotherzach/45824757d6cc65a3bf844891fbfae7d5 to your computer and use it in GitHub Desktop.
Save theotherzach/45824757d6cc65a3bf844891fbfae7d5 to your computer and use it in GitHub Desktop.
from decimal import *
# A dict has a key ane a value. In the following, they key is 'name' and the value is 'Homer'.
homer = {'name':'Homer', 'age':'39'}
# I'm using strings for the values so it looks more like the data you get from your CSV.
milhouse = {'name':'Milhouse', 'age':'10'}
unsorted_list = [homer, {'name': 'Marge', 'age': '37'}, milhouse, {'name':'Bart', 'age':'10'}]
### CLEAN UP unsorted_list
# First we need to clean up our list so our numbers are actually numbers
# http://book.pythontips.com/en/latest/map_filter.html#map
# Let's understand map first, but we need to understand functions.
def times_2(x):
return x * 2
print "The result of times_2(11): ", times_2(11)
# We can apply a function to an entire list via map like so:
junk_list = [4, 5, 6]
print "The result of map(times_2, junk_list): ", map(times_2, junk_list)
# To get back to our original goal, we want to turn our strings into numbers so we can do greater than and less than comparisons.
# Let's write a function that'll convert the age of homer or milhouse.
def converted_number_or_string(value):
try:
return Decimal(value)
except InvalidOperation:
return value
def convert_values_to_numeric(record):
result = {}
for key, value in record.iteritems():
result[key] = converted_number_or_string(value)
return result
print convert_values_to_numeric(milhouse)
# Now that we've got something that'll work for a single record, you can use map to clean up the entire unsorted_list at once.
# You should use the map function, unsorted_list and convert_age_to_int to make a new cleaned_unsorted_list now.
# TODO: convert the values in `unsorted_list`
# TODO: Be able to reproduce this file from scratch from memory
# TODO: from an empty file, get to your current point with your csv import and then create a new list called cleaned_unsorted_list
# TODO: Be able to do that last thing from memory
import csv
unsorted_list = []
with open('TechCrunchcontinentalUSA.csv', 'rbU') as csvfile:
reader = csv.DictReader(csvfile)
print type(reader)
for row in reader:
unsorted_list.append(row)
for datum in unsorted_list:
print datum
print "done"
import csv
with open('TechCrunchcontinentalUSA.csv', 'rbU') as csvfile:
reader = csv.DictReader(csvfile)
print type(reader)
for row in reader:
print row
print "done"
@theotherzach
Copy link
Author

I downloaded the csv file from here: https://support.spatialkey.com/spatialkey-sample-csv-data/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment