Last active
December 29, 2015 09:29
-
-
Save cldershem/7650277 to your computer and use it in GitHub Desktop.
This file contains 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
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)) | |
This file contains 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
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