Skip to content

Commit a032b68

Browse files
committed
Add custom delimiter option
1 parent d4eecb3 commit a032b68

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ For MongoDB (multiple JSON objects per file, which is non-standard JSON):
2424

2525
python json2csv.py --each-line /path/to/json_file.json /path/to/outline_file.json
2626

27+
For custom CSV delimiter output:
28+
29+
python json2csv.py /path/to/json_file.json /path/to/outline_file.json --csv-delimiter ';'
30+
2731
## Outline Format
2832

2933
For this JSON file:

json2csv.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def make_string(self, item):
9090
else:
9191
return unicode(item)
9292

93-
def write_csv(self, filename='output.csv', make_strings=False):
93+
def write_csv(self, filename='output.csv', make_strings=False, delimiter=','):
9494
"""Write the processed rows to the given filename
9595
"""
9696
if (len(self.rows) <= 0):
@@ -100,7 +100,7 @@ def write_csv(self, filename='output.csv', make_strings=False):
100100
else:
101101
out = self.rows
102102
with open(filename, 'wb+') as f:
103-
writer = csv.DictWriter(f, self.key_map.keys())
103+
writer = csv.DictWriter(f, self.key_map.keys(), delimiter=delimiter)
104104
writer.writeheader()
105105
writer.writerows(out)
106106

@@ -131,6 +131,8 @@ def init_parser():
131131
help="Path to csv file to output")
132132
parser.add_argument(
133133
'--strings', help="Convert lists, sets, and dictionaries fully to comma-separated strings.", action="store_true", default=True)
134+
parser.add_argument('--csv-delimiter', type=str, default=',',
135+
help="Delimiter for csv output")
134136

135137
return parser
136138

@@ -152,4 +154,4 @@ def init_parser():
152154
fileName, fileExtension = os.path.splitext(args.json_file.name)
153155
outfile = fileName + '.csv'
154156

155-
loader.write_csv(filename=outfile, make_strings=args.strings)
157+
loader.write_csv(filename=outfile, make_strings=args.strings, delimiter=args.csv_delimiter)

tests.py

+36-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
from json2csv import Json2Csv, MultiLineJson2Csv
44
from gen_outline import make_outline
5-
5+
import os
66

77
class TestJson2Csv(unittest.TestCase):
88

@@ -115,6 +115,41 @@ def test_load_bare_json(self):
115115
def test_write_csv(self):
116116
pass
117117

118+
def test_custom_delimiter(self):
119+
outline = {"map": [['author', 'source.author'], ['message', 'message.original']], "collection": "nodes"}
120+
loader = Json2Csv(outline)
121+
with open('fixtures/data.json') as f:
122+
loader.load(f)
123+
124+
filename = "test.csv"
125+
loader.write_csv(filename=filename, delimiter=';')
126+
output = open(filename, "r")
127+
csvString = output.read()
128+
129+
author = 'author'
130+
delimiterSubstring = csvString[len(author):len(author)+1]
131+
132+
self.assertEqual(delimiterSubstring, ';')
133+
134+
os.remove(filename)
135+
136+
def test_default_delimiter(self):
137+
outline = {"map": [['author', 'source.author'], ['message', 'message.original']], "collection": "nodes"}
138+
loader = Json2Csv(outline)
139+
with open('fixtures/data.json') as f:
140+
loader.load(f)
141+
142+
filename = "test.csv"
143+
loader.write_csv(filename=filename)
144+
output = open(filename, "r")
145+
csvString = output.read()
146+
147+
author = 'author'
148+
delimiterSubstring = csvString[len(author):len(author)+1]
149+
150+
self.assertEqual(delimiterSubstring, ',')
151+
152+
os.remove(filename)
118153

119154
class TestMultiLineJson2Csv(unittest.TestCase):
120155

0 commit comments

Comments
 (0)