Permalink
Browse files

python 3 support

  • Loading branch information...
vladikk committed Mar 29, 2017
1 parent f124f5f commit 39213beeea9c43ff03104d6f819a0658a89faf39
Showing with 112 additions and 23 deletions.
  1. +92 −0 .gitignore
  2. +8 −8 json2csv-tests.py
  3. +12 −15 json2csv.py
View
@@ -0,0 +1,92 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# IPython Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# dotenv
.env
# virtualenv
venv/
ENV/
# Spyder project settings
.spyderproject
# Rope project settings
.ropeproject
# VIM
*.swp
View
@@ -1,26 +1,26 @@
import unittest
import StringIO
from io import StringIO
from json2csv import to_keyvalue_pairs, dicts_to_csv
class JSON2CSVTests(unittest.TestCase):
def test_to_keyvalue_pairs_if_parameter_is_a_string_then_a_value_pair_is_built(self):
result = to_keyvalue_pairs('value1', ['key1'])
self.assertEquals(result, [('key1', 'value1')])
self.assertEqual(result, [('key1', 'value1')])
def test_to_keyvalue_pairs_if_parameter_is_a_number_then_a_value_pair_is_built(self):
result = to_keyvalue_pairs(1, ['key1'])
self.assertEquals(result, [('key1', 1)])
self.assertEqual(result, [('key1', 1)])
def test_to_keyvalue_pairs_if_parameter_is_a_list_then_pairs_are_built(self):
result = to_keyvalue_pairs(['a', 'b', 'c'], ['key'])
self.assertEquals(result, [('key_0', 'a'), ('key_1', 'b'), ('key_2', 'c')])
self.assertEqual(result, [('key_0', 'a'), ('key_1', 'b'), ('key_2', 'c')])
def test_to_keyvalue_pairs_if_parameter_is_a_dict_then_pairs_are_build(self):
result = to_keyvalue_pairs({'a': 1, 'b': 2, 'c': 3}, ['key'])
self.assertEquals(result.sort(), [('key_a', 1), ('key_b', 2), ('key_c', 3)].sort())
self.assertEqual(result.sort(), [('key_a', 1), ('key_b', 2), ('key_c', 3)].sort())
def test_dicts_to_csv(self):
output_file = StringIO.StringIO()
output_file = StringIO()
obj1 = { 'a': 'v1', 'b': 'v2'}
obj2 = { 'a': 'v"\'3', 'b': 'v4'}
@@ -29,7 +29,7 @@ def test_dicts_to_csv(self):
dicts_to_csv([obj1, obj2, obj3], output_file)
result = output_file.getvalue().strip('\r\n')
self.assertEquals('a,b,c\r\nv1,v2,\r\n"v""\'3",v4,\r\nv5,,v6', result)
self.assertEqual('a,b,c\r\nv1,v2,\r\n"v""\'3",v4,\r\nv5,,v6', result)
if __name__ == '__main__':
unittest.main()
unittest.main()
View
@@ -1,8 +1,12 @@
import logging
from itertools import chain
import json
import csv
import StringIO
from io import StringIO
import sys
from six import string_types
logger = logging.getLogger("json2csv")
def json_to_csv(input_file_path, output_file_path):
with open(input_file_path) as input_file:
@@ -13,22 +17,14 @@ def json_to_csv(input_file_path, output_file_path):
def json_to_dicts(json_str):
objects = json.loads(json_str)
def to_single_dict(lst):
result = {}
for d in lst:
for k in d.keys():
result[k] = d[k]
return result;
return [dict(to_keyvalue_pairs(obj)) for obj in objects]
def to_keyvalue_pairs(source, ancestors=[], key_delimeter='_'):
def is_sequence(arg):
return (not hasattr(arg, "strip") and hasattr(arg, "__getitem__") or hasattr(arg, "__iter__"))
return (not isinstance(arg, string_types)) and (hasattr(arg, "__getitem__") or hasattr(arg, "__iter__"))
def is_dict(arg):
return hasattr(arg, "keys")
return isinstance(arg, dict)
if is_dict(source):
result = [to_keyvalue_pairs(source[key], ancestors + [key]) for key in source.keys()]
@@ -42,13 +38,14 @@ def is_dict(arg):
def dicts_to_csv(source, output_file):
def build_row(dict_obj, keys):
return [dict_obj.get(k, "") for k in keys]
keys = sorted(set(chain.from_iterable([o.keys() for o in source])))
rows = [build_row(d, keys) for d in source]
cw = csv.writer(output_file)
cw.writerow(keys)
for row in rows:
cw.writerow([c.encode('utf-8') if isinstance(c, str) or isinstance(c, unicode) else c for c in row])
cw.writerow([c if isinstance(c, str) or isinstance(c, unicode) else c for c in row])
def write_csv(headers, rows, file):
cw = csv.writer(file)
@@ -60,8 +57,8 @@ def write_csv(headers, rows, file):
args = sys.argv
if len(args) == 3:
json_to_csv(args[1], args[2])
print 'Finished'
print('Finished')
else:
print 'Usage:'
print 'python json2csv.py "{JSON_FILE_PATH}" "{OUTPUT_FILE_PATH}"'
print('Usage:')
print('python json2csv.py "{JSON_FILE_PATH}" "{OUTPUT_FILE_PATH}"')

0 comments on commit 39213be

Please sign in to comment.