Skip to content

Commit

Permalink
Merge pull request #35 from alingse/add-feat-array
Browse files Browse the repository at this point in the history
正确的使用 unicodecsv
  • Loading branch information
alingse authored Sep 23, 2019
2 parents 65ce296 + cd157b4 commit 16bf44e
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 37 deletions.
2 changes: 1 addition & 1 deletion jsoncsv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

import sys

PY3 = bool(sys.version_info[0] == 3)
PY2 = bool(sys.version_info[0] == 2)
16 changes: 1 addition & 15 deletions jsoncsv/dumptool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@
# author@alingse
# 2015.10.09

import six
import unicodecsv as csv
import json
import xlwt

from jsoncsv import PY3


class Dump(object):

Expand Down Expand Up @@ -96,12 +93,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):
Expand All @@ -113,12 +105,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

Expand Down
28 changes: 15 additions & 13 deletions jsoncsv/jsontool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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:
Expand All @@ -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'))
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions tests/test_jsontool.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down

0 comments on commit 16bf44e

Please sign in to comment.