Thursday, September 3, 2015

CSV reader examples for python 3

Among other things, I like python. I quite like the Talk Python to Me podcast, done by Michael Kennedy. He is, among other things, a python trainer. Among his online python training videos is one on parsing csv (comma separated value) files. His example didn't actually use the python csv module, so I whipped up a couple of examples that used csv.reader and csv.DictReader.

These use python 3.4.

=================== csv.reader example: #!c:\\anaconda3\\python.exe '''taken from https://www.youtube.com/watch?v=qajONCIvhEc https://support.spatialkey.com/spatialkey-sample-csv-data/ http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv ''' import csv import os def main(): print("Hello File I/O minicast using csv.reader") filename = os.path.abspath(os.path.join("data", "Sacramentorealestatetransactions.csv")) print(filename) with open(filename, 'r') as csvfile: fin = csv.reader(csvfile, delimiter=',') header = next(fin) print(header) # ['street', 'city', 'zip', 'state', 'beds', 'baths', 'sq__ft', 'type', 'sale_date', 'price', 'latitude', 'longitude'] entries = [] for line in fin: # lines look like # ['3526 HIGH ST', 'SACRAMENTO', '95838', 'CA', '2', '1', '836', 'Residential', 'Wed May 21 00:00:00 EDT 2008', '59222', '38.631913', '-121.434879'] row = dict() for i, h in enumerate(header): row[h] = line[i] entries.append(row) entries.sort(key= lambda r: -1*int(r['price'])) # Entries are a list of dicts. Each line looks like: # {'street': '9401 BARREL RACER CT', 'state': 'CA', 'sq__ft': '4400', 'city': 'WILTON', 'beds': '4', 'baths': '3', 'sale_date': 'Fri May 16 00:00:00 EDT 2008', 'price': '884790', 'longitude': '-121.194858', 'latitude': '38.415298', 'zip': '95693', 'type': 'Residential'} for e in entries[:5]: print("{0} beds, {1} baths sold for ${2:,}".format( e['beds'], e['baths'], int(e['price']) )) if _name_ == '__main__': main()

======================= csv.DictReader example:
#!c:\\anaconda3\\python.exe '''taken from https://www.youtube.com/watch?v=qajONCIvhEc https://support.spatialkey.com/spatialkey-sample-csv-data/ http://samplecsvs.s3.amazonaws.com/Sacramentorealestatetransactions.csv ''' import csv import os def main(): print("Hello File I/O minicast using csv.DictReader") filename = os.path.abspath(os.path.join("data", "Sacramentorealestatetransactions.csv")) print(filename) with open(filename, 'r') as csvfile: fin = csv.DictReader(csvfile, delimiter=',') print(fin.fieldnames) # ['street', 'city', 'zip', 'state', 'beds', 'baths', 'sq__ft', 'type', 'sale_date', 'price', 'latitude', 'longitude'] entries = list(fin) # lines/rows in fin look like # {'street': '3882 YELLOWSTONE LN', 'state': 'CA', 'sq__ft': '1362', 'city': 'EL DORADO HILLS', 'beds': '3', 'baths': '2', 'sale_date': 'Thu May 15 00:00:00 EDT 2008', 'price': '235738', 'longitude': '-121.075915', 'latitude': '38.655245', 'zip': '95762', 'type': 'Residential'} entries.sort(key= lambda r: -1*int(r['price'])) for e in entries[:5]: print("{0} beds, {1} baths sold for ${2:,}".format( e['beds'], e['baths'], int(e['price']) )) if _name_ == '__main__': main()