Last active
December 20, 2015 20:18
-
-
Save jonchretien/6189019 to your computer and use it in GitHub Desktop.
CSV to JSON converter for government data set.
This file contains hidden or 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/python | |
import os | |
import csv | |
import json | |
import fileinput | |
import glob | |
class CSVToJSONConverter(): | |
"""Converts CSV data to JSON format""" | |
def initialize(self, state): | |
self.update_header(state) | |
self.create_json(state) | |
def update_header(self, state): | |
header = 'state,gender,dob,name,quantity' | |
# open the csv file and read contents | |
with open(state + '.TXT', 'r+') as f: | |
old = f.read() | |
first_line = old.split('\n', 1)[0] | |
# prepend header if it doesn't exist | |
if first_line != header: | |
f.seek(0) # rewind | |
f.write(header + '\n' + old) # write the new line before | |
def create_json(self, state): | |
# open the csv file | |
source_file = open(state + '.TXT','rU') | |
data = csv.DictReader(source_file) | |
parsed_data = [] | |
# iterate through file and push rows to array | |
for row in data: | |
parsed_data.append(row) | |
source_file.close() | |
json_list = json.dumps(parsed_data) | |
# create JSON file | |
results = open('../json/' + state + '.json','wb') | |
results.write(json_list) | |
results.close() | |
# create instance of CSVToJSONConverter class | |
fileConverter = CSVToJSONConverter() | |
# change working directory | |
os.chdir('csv') | |
# find all files that end in .TXT and convert to JSON format | |
for files in glob.glob('*.TXT'): | |
strg = files.replace('.TXT', '') | |
print 'Converting ' + strg | |
fileConverter.initialize(strg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment