-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad072e4
commit 68c2b39
Showing
1 changed file
with
21 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,32 @@ | ||
import os | ||
import json | ||
import csv | ||
from pprint import pprint | ||
|
||
def get_output(input_type): | ||
|
||
def get_output(input_type, output_dir): | ||
if input_type == 'print': | ||
return _print | ||
elif input_type == 'save_json': | ||
return _save_json | ||
return lambda results: _save_json(results, output_dir) | ||
elif input_type == 'save_csv': | ||
return _save_csv | ||
return lambda results: _save_csv(results, output_dir) | ||
|
||
|
||
def _print(): | ||
pass | ||
def _print(results, *args): | ||
pprint(results) | ||
|
||
|
||
def _save_json(): | ||
pass | ||
def _save_json(results, output_dir): | ||
filename = os.path.join(output_dir, 'output.json') | ||
with open(filename, 'w') as json_file: | ||
json.dump(results, json_file, indent=4) | ||
|
||
|
||
def _save_csv(): | ||
pass | ||
def _save_csv(results, output_dir): | ||
filename = os.path.join(output_dir, 'output.csv') | ||
keys = results[0].keys() if results else [] | ||
with open(filename, 'w', newline='') as csv_file: | ||
writer = csv.DictWriter(csv_file, fieldnames=keys) | ||
writer.writeheader() | ||
writer.writerows(results) |