Skip to content

Commit

Permalink
Fix writing of multiple strings in a list (elastic#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
hub-cap authored May 13, 2020
1 parent 5896b89 commit 6774710
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion anonymize_it/writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 7 additions & 5 deletions test_anonymize_it/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit 6774710

Please sign in to comment.