From fac325e75622194296793d09bf98bf6cf9347a70 Mon Sep 17 00:00:00 2001 From: alingse Date: Tue, 24 Sep 2019 01:12:21 +0800 Subject: [PATCH 1/2] use unicodecsv correct --- jsoncsv/__init__.py | 2 +- jsoncsv/dumptool.py | 15 ++------------- jsoncsv/jsontool.py | 28 +++++++++++++++------------- tests/test_jsontool.py | 14 +++++++------- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/jsoncsv/__init__.py b/jsoncsv/__init__.py index 480486a..e604bba 100644 --- a/jsoncsv/__init__.py +++ b/jsoncsv/__init__.py @@ -4,4 +4,4 @@ import sys -PY3 = bool(sys.version_info[0] == 3) +PY2 = bool(sys.version_info[0] == 2) diff --git a/jsoncsv/dumptool.py b/jsoncsv/dumptool.py index d3b85bb..579f3c0 100755 --- a/jsoncsv/dumptool.py +++ b/jsoncsv/dumptool.py @@ -7,7 +7,7 @@ import json import xlwt -from jsoncsv import PY3 +from jsoncsv import PY2 class Dump(object): @@ -96,12 +96,7 @@ def initialize(self, **kwargs): self.csv_writer = None def write_headers(self): - if not PY3: - # Python 2 csv does not support unicode - fieldnames = [header.encode('utf8') for header in self._headers] - else: - fieldnames = self._headers - self.csv_writer = csv.DictWriter(self.fout, fieldnames) + self.csv_writer = csv.DictWriter(self.fout, self._headers) self.csv_writer.writeheader() def write_obj(self, obj): @@ -113,12 +108,6 @@ def patch_obj(self, obj): if value in [None, {}, []]: value = "" - if not PY3: - # Python 2 csv does not support unicode - key = key.encode('utf8') - if isinstance(value, six.text_type): - value = value.encode('utf8') - new_obj[key] = value return new_obj diff --git a/jsoncsv/jsontool.py b/jsoncsv/jsontool.py index 2eafa2b..5765e97 100755 --- a/jsoncsv/jsontool.py +++ b/jsoncsv/jsontool.py @@ -10,7 +10,7 @@ from itertools import groupby from operator import itemgetter -from jsoncsv import PY3 +from jsoncsv import PY2 from jsoncsv.utils import encode_safe_key from jsoncsv.utils import decode_safe_key @@ -32,10 +32,11 @@ def gen_leaf(root, path=None): yield leaf else: if isinstance(root, dict): - if PY3: - items = root.items() - else: + if PY2: items = root.iteritems() + else: + items = root.items() + else: items = enumerate(root) @@ -96,10 +97,10 @@ def expand(origin, separator='.', safe=False): expobj = {} for path, value in leafs: - if PY3: - path = map(str, path) - else: + if PY2: path = map(unicode, path) # noqa + else: + path = map(str, path) if safe: key = encode_safe_key(path, separator) @@ -113,10 +114,10 @@ def expand(origin, separator='.', safe=False): def restore(expobj, separator='.', safe=False): leafs = [] - if PY3: - items = expobj.items() - else: + if PY2: items = expobj.iteritems() + else: + items = expobj.items() for key, value in items: if safe: @@ -141,8 +142,9 @@ def convert_json(fin, fout, func, separator=".", safe=False): obj = json.loads(line) new = func(obj, separator=separator, safe=safe) content = json.dumps(new, ensure_ascii=False) - if PY3: - fout.write(content) - else: + if PY2: fout.write(content.encode('utf-8')) + else: + fout.write(content) + fout.write(str('\n')) diff --git a/tests/test_jsontool.py b/tests/test_jsontool.py index 3892690..19f0a8f 100644 --- a/tests/test_jsontool.py +++ b/tests/test_jsontool.py @@ -6,7 +6,7 @@ import io import unittest -from jsoncsv import PY3 +from jsoncsv import PY2 from jsoncsv.jsontool import expand, restore from jsoncsv.jsontool import is_array from jsoncsv.jsontool import convert_json @@ -95,10 +95,10 @@ class TestConvertJSON(unittest.TestCase): def test_convert_expand(self): fin = io.StringIO('{"a":{"b":3}}\n{"a":{"c":4}}\n') - if PY3: - fout = io.StringIO() - else: + if PY2: fout = io.BytesIO() + else: + fout = io.StringIO() convert_json(fin, fout, expand) @@ -109,10 +109,10 @@ def test_convert_expand(self): def test_convert_restore(self): fin = io.StringIO('{"a.b": 3}\n{"a.c": 4}\n') - if PY3: - fout = io.StringIO() - else: + if PY2: fout = io.BytesIO() + else: + fout = io.StringIO() convert_json(fin, fout, restore) From cd157b4d44c83fbe1489a5987f7a861f706c1fd8 Mon Sep 17 00:00:00 2001 From: alingse Date: Tue, 24 Sep 2019 01:12:35 +0800 Subject: [PATCH 2/2] remove py2 --- jsoncsv/dumptool.py | 3 --- setup.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/jsoncsv/dumptool.py b/jsoncsv/dumptool.py index 579f3c0..a776e87 100755 --- a/jsoncsv/dumptool.py +++ b/jsoncsv/dumptool.py @@ -2,13 +2,10 @@ # author@alingse # 2015.10.09 -import six import unicodecsv as csv import json import xlwt -from jsoncsv import PY2 - class Dump(object): diff --git a/setup.py b/setup.py index 11d920b..ad134d9 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setup( name='jsoncsv', - version='2.2.0', + version='2.2.1', url='https://github.com/alingse/jsoncsv', description='A command tool easily convert json file to csv or xlsx.', long_description=readme,