Skip to content

Instantly share code, notes, and snippets.

@cldershem
Last active December 29, 2015 09:29
Show Gist options
  • Save cldershem/7650277 to your computer and use it in GitHub Desktop.
Save cldershem/7650277 to your computer and use it in GitHub Desktop.
def get_dict(test_file):
"""
Takes a csv as test_file and using first line as header, returns dict.
"""
with open(test_file, 'r') as f:
keys = next(f).strip().split(';')
for line in f:
line = dict(zip(keys, line.strip().split(';')))
yield line
def get_json(test_file):
"""
Takes a csv as test_file and using first line as header, returns json.
"""
import json
with open(test_file, 'r') as f:
keys = next(f).strip().split(';')
for line in f:
line = dict(zip(keys, line.strip().split(';')))
yield json.dumps(line)
if __name__ == '__main__':
TEST_FILE = 'test_file.txt'
test_dict = get_dict(TEST_FILE).next()
test_json = get_json(TEST_FILE).next()
if test_dict == test_json:
print "dict == json"
else:
print "dict type" + str(type(test_dict))
print "json type" + str(type(test_json))
date;db;num1;num2;num3;num4
01/05/2013;D1;123456;789124;4567;79846541
01/05/2013;D1;143543;5951;2564;45435
02/15/2013;D2;8765132;7596565;65876541;654
08/24/2013;D1;768631;71531;753;767565
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment