Last active
September 5, 2024 15:57
-
-
Save adamabernathy/a903910a7a95b4090bce8bb252f751ed to your computer and use it in GitHub Desktop.
Quick and dirty CSV to JSON converter. Does not chunk files, so really large files might be an issue depending on available memory.
This file contains hidden or 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
| import csv | |
| import json | |
| import argparse | |
| def convert_csv_to_json(input_file, output_file, verbose=False): | |
| print(f"Converting {input_file} >>> {output_file}") | |
| output_dict = { "rows": [], "sourceFile": input_file } | |
| # !!! Assumes a CRLF/UTF-8 encoded file | |
| converted_rows = 0 | |
| with open(input_file, "r", encoding="utf-8") as f: | |
| for row in csv.reader(f): | |
| if converted_rows == 0: | |
| headers = row # Assumes CSV has a headers row. | |
| print(f"Detected colummn headers: {headers}") | |
| else: | |
| output_dict["rows"].append( dict(zip(headers, row)) ) | |
| converted_rows += 1 | |
| output_dict["convertedRows"] = converted_rows | |
| if verbose: | |
| print(json.dumps(output_dict, indent=2)) | |
| with open(output_file, "w", encoding="utf-8") as f: | |
| json.dump(output_dict, f, ensure_ascii=False, indent=2) | |
| # | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser( | |
| prog="Quick and Dirty CSV to JSON parser", | |
| description="Converts a CRLF UTF-8 CSV file to a JSON object.", | |
| epilog="Please don't feed the wild animals") | |
| parser.add_argument("-f", "--input-file") | |
| parser.add_argument("-o", "--output-file") | |
| parser.add_argument("-v", "--verbose", action="store_true") | |
| args = parser.parse_args() | |
| INPUT_FILE = args.input_file | |
| OUTPUT_FILE = args.output_file \ | |
| if args.output_file is not None \ | |
| else f"{'.'.join(args.input_file.split('.')[:-1])}.json" | |
| VERBOSE = args.verbose | |
| convert_csv_to_json(INPUT_FILE, OUTPUT_FILE, VERBOSE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment