Skip to content

Commit 704fa50

Browse files
committed
使用 python csv 包
1 parent 6d03146 commit 704fa50

10 files changed

Lines changed: 65 additions & 52 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:

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2019.07.14
2+
3+
- 2.2.0 use python csv package
4+
15
2019.06.12
26

37
- 2.1.0 add convert_json && dump_excel to use as library

fixture/files/expand.0.json

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

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: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# author@alingse
33
# 2015.10.09
44

5+
import csv
56
import json
67
import xlwt
78

@@ -18,15 +19,6 @@ def __init__(self, fin, fout, **kwargs):
1819
def initialize(self, **kwargs):
1920
pass
2021

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-
3022
def prepare(self):
3123
pass
3224

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

5244
# read
5345
if not read_row or read_row < 1:
54-
read_row = 1
46+
read_row = -1
5547

5648
for line in fin:
5749
obj = json.loads(line)
@@ -100,35 +92,31 @@ class DumpCSV(DumpExcel):
10092

10193
def initialize(self, **kwargs):
10294
super(DumpCSV, self).initialize(**kwargs)
103-
104-
self._separator = kwargs.get('separator', ',')
95+
self.csv_writer = None
10596

10697
def write_headers(self):
107-
header = self._separator.join(self._headers)
108-
if PY3:
109-
self.fout.write(header)
98+
if not PY3:
99+
fieldnames = [header.encode('utf8') for header in self._headers]
110100
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)
118-
else:
119-
return unicode(value) # noqa
101+
fieldnames = self._headers
102+
self.csv_writer = csv.DictWriter(self.fout, fieldnames)
103+
self.csv_writer.writeheader()
120104

121105
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')
106+
self.csv_writer.writerow(self.patch_obj(obj))
107+
108+
def patch_obj(self, obj):
109+
new_obj = {}
110+
for key, value in obj.items():
111+
if value in [None, {}, []]:
112+
value = ""
113+
if not PY3:
114+
key = key.encode('utf8')
115+
if isinstance(value, unicode):
116+
value = value.encode('utf8')
117+
118+
new_obj[key] = value
119+
return new_obj
132120

133121

134122
class DumpXLS(DumpExcel):
@@ -152,7 +140,7 @@ def write_obj(self, obj):
152140
self.cloumn = 0
153141

154142
for head in self._headers:
155-
value = self.patch(obj.get(head))
143+
value = obj.get(head)
156144
self.ws.write(self.row, self.cloumn, value)
157145
self.cloumn += 1
158146

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', 'wb')
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)