Skip to content

Commit 16bf44e

Browse files
authored
Merge pull request #35 from alingse/add-feat-array
正确的使用 unicodecsv
2 parents 65ce296 + cd157b4 commit 16bf44e

File tree

5 files changed

+25
-37
lines changed

5 files changed

+25
-37
lines changed

Diff for: jsoncsv/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
import sys
66

7-
PY3 = bool(sys.version_info[0] == 3)
7+
PY2 = bool(sys.version_info[0] == 2)

Diff for: jsoncsv/dumptool.py

+1-15
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22
# author@alingse
33
# 2015.10.09
44

5-
import six
65
import unicodecsv as csv
76
import json
87
import xlwt
98

10-
from jsoncsv import PY3
11-
129

1310
class Dump(object):
1411

@@ -96,12 +93,7 @@ def initialize(self, **kwargs):
9693
self.csv_writer = None
9794

9895
def write_headers(self):
99-
if not PY3:
100-
# Python 2 csv does not support unicode
101-
fieldnames = [header.encode('utf8') for header in self._headers]
102-
else:
103-
fieldnames = self._headers
104-
self.csv_writer = csv.DictWriter(self.fout, fieldnames)
96+
self.csv_writer = csv.DictWriter(self.fout, self._headers)
10597
self.csv_writer.writeheader()
10698

10799
def write_obj(self, obj):
@@ -113,12 +105,6 @@ def patch_obj(self, obj):
113105
if value in [None, {}, []]:
114106
value = ""
115107

116-
if not PY3:
117-
# Python 2 csv does not support unicode
118-
key = key.encode('utf8')
119-
if isinstance(value, six.text_type):
120-
value = value.encode('utf8')
121-
122108
new_obj[key] = value
123109
return new_obj
124110

Diff for: jsoncsv/jsontool.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from itertools import groupby
1111
from operator import itemgetter
1212

13-
from jsoncsv import PY3
13+
from jsoncsv import PY2
1414
from jsoncsv.utils import encode_safe_key
1515
from jsoncsv.utils import decode_safe_key
1616

@@ -32,10 +32,11 @@ def gen_leaf(root, path=None):
3232
yield leaf
3333
else:
3434
if isinstance(root, dict):
35-
if PY3:
36-
items = root.items()
37-
else:
35+
if PY2:
3836
items = root.iteritems()
37+
else:
38+
items = root.items()
39+
3940
else:
4041
items = enumerate(root)
4142

@@ -96,10 +97,10 @@ def expand(origin, separator='.', safe=False):
9697

9798
expobj = {}
9899
for path, value in leafs:
99-
if PY3:
100-
path = map(str, path)
101-
else:
100+
if PY2:
102101
path = map(unicode, path) # noqa
102+
else:
103+
path = map(str, path)
103104

104105
if safe:
105106
key = encode_safe_key(path, separator)
@@ -113,10 +114,10 @@ def expand(origin, separator='.', safe=False):
113114
def restore(expobj, separator='.', safe=False):
114115
leafs = []
115116

116-
if PY3:
117-
items = expobj.items()
118-
else:
117+
if PY2:
119118
items = expobj.iteritems()
119+
else:
120+
items = expobj.items()
120121

121122
for key, value in items:
122123
if safe:
@@ -141,8 +142,9 @@ def convert_json(fin, fout, func, separator=".", safe=False):
141142
obj = json.loads(line)
142143
new = func(obj, separator=separator, safe=safe)
143144
content = json.dumps(new, ensure_ascii=False)
144-
if PY3:
145-
fout.write(content)
146-
else:
145+
if PY2:
147146
fout.write(content.encode('utf-8'))
147+
else:
148+
fout.write(content)
149+
148150
fout.write(str('\n'))

Diff for: setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='jsoncsv',
15-
version='2.2.0',
15+
version='2.2.1',
1616
url='https://github.com/alingse/jsoncsv',
1717
description='A command tool easily convert json file to csv or xlsx.',
1818
long_description=readme,

Diff for: tests/test_jsontool.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import io
77
import unittest
88

9-
from jsoncsv import PY3
9+
from jsoncsv import PY2
1010
from jsoncsv.jsontool import expand, restore
1111
from jsoncsv.jsontool import is_array
1212
from jsoncsv.jsontool import convert_json
@@ -95,10 +95,10 @@ class TestConvertJSON(unittest.TestCase):
9595

9696
def test_convert_expand(self):
9797
fin = io.StringIO('{"a":{"b":3}}\n{"a":{"c":4}}\n')
98-
if PY3:
99-
fout = io.StringIO()
100-
else:
98+
if PY2:
10199
fout = io.BytesIO()
100+
else:
101+
fout = io.StringIO()
102102

103103
convert_json(fin, fout, expand)
104104

@@ -109,10 +109,10 @@ def test_convert_expand(self):
109109

110110
def test_convert_restore(self):
111111
fin = io.StringIO('{"a.b": 3}\n{"a.c": 4}\n')
112-
if PY3:
113-
fout = io.StringIO()
114-
else:
112+
if PY2:
115113
fout = io.BytesIO()
114+
else:
115+
fout = io.StringIO()
116116

117117
convert_json(fin, fout, restore)
118118

0 commit comments

Comments
 (0)