From 6774710f7328a09dad5bdc95c6a0ca22ef200323 Mon Sep 17 00:00:00 2001 From: Michael Basnight Date: Wed, 13 May 2020 09:55:37 -0500 Subject: [PATCH] Fix writing of multiple strings in a list (#21) --- anonymize_it/writers.py | 3 ++- test_anonymize_it/test_writers.py | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/anonymize_it/writers.py b/anonymize_it/writers.py index d7aca33..b154da6 100644 --- a/anonymize_it/writers.py +++ b/anonymize_it/writers.py @@ -32,7 +32,8 @@ def write_data(self, data, file_name=None): dir_path = os.path.join(os.path.abspath(os.getcwd()), self.out_dir) os.makedirs(dir_path, exist_ok=True) with open("{}/{}.json".format(dir_path, file_name), 'w') as f: - f.write(data) + for d in data: + f.write('{}\n'.format(d)) class GCSWriter(BaseWriter): diff --git a/test_anonymize_it/test_writers.py b/test_anonymize_it/test_writers.py index 0325fd1..7f08046 100644 --- a/test_anonymize_it/test_writers.py +++ b/test_anonymize_it/test_writers.py @@ -8,16 +8,18 @@ def test_fswriter(): "directory": "test_output" } - data = { - "test": "this is a test".split() - } + data = [ + json.dumps({"this": "is the json header for a elasticsearch bulk"}), + json.dumps({"test": "this is a test".split()}) + ] fswriter = writers.FSWriter(params) file_name = "output" - fswriter.write_data(json.dumps(data), file_name=file_name) + fswriter.write_data(data, file_name=file_name) dir_path = os.path.join(os.path.abspath(os.getcwd()), fswriter.out_dir) with open(f'{dir_path}/{file_name}.json', 'r') as f: - in_data = json.loads(f.read()) + # The list has one extra newline which splits to a new row, so we remove it + in_data = f.read().split("\n")[:-1] os.remove(f'{dir_path}/{file_name}.json')