Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom csv delimiter option #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ For MongoDB (multiple JSON objects per file, which is non-standard JSON):

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

For custom CSV delimiter output:

python json2csv.py /path/to/json_file.json /path/to/outline_file.json --csv-delimiter ';'

## Outline Format

For this JSON file:
Expand Down
8 changes: 5 additions & 3 deletions json2csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def make_string(self, item):
else:
return unicode(item)

def write_csv(self, filename='output.csv', make_strings=False):
def write_csv(self, filename='output.csv', make_strings=False, delimiter=','):
"""Write the processed rows to the given filename
"""
if (len(self.rows) <= 0):
Expand All @@ -100,7 +100,7 @@ def write_csv(self, filename='output.csv', make_strings=False):
else:
out = self.rows
with open(filename, 'wb+') as f:
writer = csv.DictWriter(f, self.key_map.keys())
writer = csv.DictWriter(f, self.key_map.keys(), delimiter=delimiter)
writer.writeheader()
writer.writerows(out)

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

return parser

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

loader.write_csv(filename=outfile, make_strings=args.strings)
loader.write_csv(filename=outfile, make_strings=args.strings, delimiter=args.csv_delimiter)
37 changes: 36 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
from json2csv import Json2Csv, MultiLineJson2Csv
from gen_outline import make_outline

import os

class TestJson2Csv(unittest.TestCase):

Expand Down Expand Up @@ -115,6 +115,41 @@ def test_load_bare_json(self):
def test_write_csv(self):
pass

def test_custom_delimiter(self):
outline = {"map": [['author', 'source.author'], ['message', 'message.original']], "collection": "nodes"}
loader = Json2Csv(outline)
with open('fixtures/data.json') as f:
loader.load(f)

filename = "test.csv"
loader.write_csv(filename=filename, delimiter=';')
output = open(filename, "r")
csvString = output.read()

author = 'author'
delimiterSubstring = csvString[len(author):len(author)+1]

self.assertEqual(delimiterSubstring, ';')

os.remove(filename)

def test_default_delimiter(self):
outline = {"map": [['author', 'source.author'], ['message', 'message.original']], "collection": "nodes"}
loader = Json2Csv(outline)
with open('fixtures/data.json') as f:
loader.load(f)

filename = "test.csv"
loader.write_csv(filename=filename)
output = open(filename, "r")
csvString = output.read()

author = 'author'
delimiterSubstring = csvString[len(author):len(author)+1]

self.assertEqual(delimiterSubstring, ',')

os.remove(filename)

class TestMultiLineJson2Csv(unittest.TestCase):

Expand Down