From 2d5e23c8a9773556472f6d832304ecf298cd53bd Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Thu, 18 Apr 2024 12:43:22 +0200 Subject: [PATCH] feat: added script to convert json strings to comma seperated lists --- scripts/json_array_to_list | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 scripts/json_array_to_list diff --git a/scripts/json_array_to_list b/scripts/json_array_to_list new file mode 100755 index 0000000..8fb258c --- /dev/null +++ b/scripts/json_array_to_list @@ -0,0 +1,26 @@ +#!/bin/python3 +from pathlib import Path +import tempfile +import subprocess + +if __name__ == "__main__": + # set input_string to the contents of a temporary file opened with nano + f = tempfile.NamedTemporaryFile(mode='w+t', delete=False) + n = f.name + f.close() + subprocess.call(['nano', n]) + + with open(n, 'r') as f: + input_string = f.read() + Path(n).unlink() + + # Write your code here + input_string = input_string.strip().strip(',').strip('[').strip(']') + + def process_part(part: str) -> str: + part = part.strip() + quotation = '"' if part.startswith('"') and part.endswith('"') else "'" + return part.strip(quotation) + + input_string = ','.join(process_part(p) for p in input_string.split(',')) + print(input_string) \ No newline at end of file