Skip to content

Commit c729edd

Browse files
authored
Merge pull request #31 from alingse/use-csv-library
use python csv package
2 parents 6d03146 + 64eabe7 commit c729edd

9 files changed

Lines changed: 69 additions & 51 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ script:
1616
- python setup.py install
1717
- cat fixture/files/raw.0.json|jsoncsv -e > fixture/files/tmp.expand.0.json
1818
- cat fixture/files/raw.0.json|jsoncsv -e|jsoncsv -r|jsoncsv > fixture/files/tmp.expand.0.json
19-
- cat fixture/files/raw.2.json|jsoncsv|mkexcel -t xls > fixture/files/tmp.output.2.xls
20-
- cat fixture/files/raw.2.json|jsoncsv|mkexcel -t csv > fixture/files/tmp.output.2..csv
19+
- cat fixture/files/expand.2.json|jsoncsv|mkexcel -t xls > fixture/files/tmp.output.2.xls
20+
- cat fixture/files/expand.2.json|jsoncsv|mkexcel -t csv > fixture/files/tmp.output.2..csv
2121
after_success:
2222
- coveralls
2323
notifications:

fixture/files/expand.2.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{"河流名字": "长江", "河流长度": "6000千米"}
2+
{"河流名字": "黄河", "河流长度": "5000千米", "Area": 300.0}
3+
{"河流名字": "珠江", "Out": "南海"}
4+
{"河流名字": "Hanjiang", "Source": "HanZhong", "Out": "长江"}

fixture/files/output.1.csv

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
detail.what,detail.when,detail.where,detail.who.0,detail.who.1,detail.who.2,title
2-
eat,2016,beijing,one,two,three,AAAA
3-
play,2016,湖北,one,two,three,BBBB
4-
,2015,chengdu,two,three,,CCCC
5-
happy,2015,,,,,DDDD
1+
detail.how,detail.what,detail.when,detail.where,detail.who.0,detail.who.1,detail.who.2,title
2+
,eat,2016,beijing,one,two,three,AAAA
3+
offline,play,2016,湖北,one,two,three,BBBB
4+
,,2015,chengdu,two,three,,CCCC
5+
online,happy,2015,,,,,DDDD

fixture/files/output.1.sort.csv

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
detail.what,detail.when,detail.where,detail.who.0,detail.who.1,detail.who.2,title
2-
eat,2016,beijing,one,two,three,AAAA
3-
play,2016,湖北,one,two,three,BBBB
4-
,2015,chengdu,two,three,,CCCC
5-
happy,2015,,,,,DDDD
1+
detail.how,detail.what,detail.when,detail.where,detail.who.0,detail.who.1,detail.who.2,title
2+
,eat,2016,beijing,one,two,three,AAAA
3+
offline,play,2016,湖北,one,two,three,BBBB
4+
,,2015,chengdu,two,three,,CCCC
5+
online,happy,2015,,,,,DDDD

fixture/files/raw.2.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

jsoncsv/dumptool.py

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# author@alingse
33
# 2015.10.09
44

5+
import six
6+
import csv
57
import json
68
import xlwt
79

@@ -18,15 +20,6 @@ def __init__(self, fin, fout, **kwargs):
1820
def initialize(self, **kwargs):
1921
pass
2022

21-
def patch(self, value):
22-
if value is None:
23-
return ''
24-
25-
if value == {} or value == []:
26-
return ''
27-
28-
return value
29-
3023
def prepare(self):
3124
pass
3225

@@ -51,7 +44,7 @@ def load_headers(fin, read_row=None, sort_type=None):
5144

5245
# read
5346
if not read_row or read_row < 1:
54-
read_row = 1
47+
read_row = -1
5548

5649
for line in fin:
5750
obj = json.loads(line)
@@ -100,35 +93,34 @@ class DumpCSV(DumpExcel):
10093

10194
def initialize(self, **kwargs):
10295
super(DumpCSV, self).initialize(**kwargs)
103-
104-
self._separator = kwargs.get('separator', ',')
96+
self.csv_writer = None
10597

10698
def write_headers(self):
107-
header = self._separator.join(self._headers)
108-
if PY3:
109-
self.fout.write(header)
110-
else:
111-
self.fout.write(header.encode('utf-8'))
112-
self.fout.write('\n')
113-
114-
def patch(self, value):
115-
value = super(DumpCSV, self).patch(value)
116-
if PY3:
117-
return str(value)
99+
if not PY3:
100+
# Python 2 csv does not support unicode
101+
fieldnames = [header.encode('utf8') for header in self._headers]
118102
else:
119-
return unicode(value) # noqa
103+
fieldnames = self._headers
104+
self.csv_writer = csv.DictWriter(self.fout, fieldnames)
105+
self.csv_writer.writeheader()
120106

121107
def write_obj(self, obj):
122-
values = [
123-
self.patch(obj.get(head))
124-
for head in self._headers
125-
]
126-
content = self._separator.join(values)
127-
if PY3:
128-
self.fout.write(content)
129-
else:
130-
self.fout.write(content.encode('utf-8'))
131-
self.fout.write('\n')
108+
self.csv_writer.writerow(self.patch_obj(obj))
109+
110+
def patch_obj(self, obj):
111+
new_obj = {}
112+
for key, value in obj.items():
113+
if value in [None, {}, []]:
114+
value = ""
115+
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+
122+
new_obj[key] = value
123+
return new_obj
132124

133125

134126
class DumpXLS(DumpExcel):
@@ -152,7 +144,7 @@ def write_obj(self, obj):
152144
self.cloumn = 0
153145

154146
for head in self._headers:
155-
value = self.patch(obj.get(head))
147+
value = obj.get(head)
156148
self.ws.write(self.row, self.cloumn, value)
157149
self.cloumn += 1
158150

jsoncsv/main.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# coding=utf-8
22

3+
import io
34
import click
45
import sys
56

67
from jsoncsv import jsontool
78
from jsoncsv import dumptool
9+
from jsoncsv import PY3
810
from jsoncsv.dumptool import dump_excel
911
from jsoncsv.jsontool import convert_json
1012
from jsoncsv.utils import separator_type
@@ -86,8 +88,11 @@ def jsoncsv(output, input, expand, restore, safe, separator):
8688
type=click.File('wb'),
8789
default=sys.stdout)
8890
def mkexcel(output, input, sort_, row, type_):
89-
if output == sys.stdout and type_ == "xls":
91+
if type_ == "xls" and output == sys.stdout:
9092
output = click.get_binary_stream('stdout')
93+
if type_ == "csv" and output != sys.stdout:
94+
if PY3:
95+
output = io.TextIOWrapper(output)
9196

9297
klass = dumptool.DumpCSV
9398
if type_ == "xls":

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

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

tests/test_mkexcel.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from jsoncsv.dumptool import dump_excel
1010

1111

12-
class Testdumptool(unittest.TestCase):
12+
class TestDumpTool(unittest.TestCase):
1313

1414
# FIXME (使用虚拟文件)
1515
def test_dumpexcel_csv(self):
@@ -52,3 +52,21 @@ def test_dumpcexcel_xls(self):
5252

5353
fin.close()
5454
fout.close()
55+
56+
def test_dump_csv_with_non_ascii(self):
57+
fin = open('./fixture/files/expand.2.json', 'r')
58+
fout = open('./fixture/files/tmp.output.2.csv', 'w')
59+
60+
dump_excel(fin, fout, DumpCSV)
61+
62+
fin.close()
63+
fout.close()
64+
65+
def test_dump_xls_with_non_ascii(self):
66+
fin = open('./fixture/files/expand.2.json', 'r')
67+
fout = open('./fixture/files/tmp.output.2.xls', 'wb')
68+
69+
dump_excel(fin, fout, DumpXLS)
70+
71+
fin.close()
72+
fout.close()

0 commit comments

Comments
 (0)