From 4c3332f07fb099b05c7d636b06574973ddd75b58 Mon Sep 17 00:00:00 2001 From: leftkats Date: Fri, 6 Jun 2025 16:04:26 +0300 Subject: [PATCH] feat: csv to ndjson converter script :sparkles: --- CONTRIBUTING.md | 2 +- CSV_TO_NDJSON/README.md | 38 +++++++++++++++++++++++++++++++++ CSV_TO_NDJSON/csv_to_ndjson.py | 39 ++++++++++++++++++++++++++++++++++ README.md | 1 + 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 CSV_TO_NDJSON/README.md create mode 100644 CSV_TO_NDJSON/csv_to_ndjson.py diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 68ebeda..c278f13 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -28,4 +28,4 @@ If this is your first time contributing to an open source project, check out thi ## Code of Conduct -We strive to maintain a welcoming and inclusive community. Please read and respect our [Code of Conduct](https://github.com/DhanushNehru/Python-Scripts/blob/master/CODE_OF_CONDUCT.md) in all your interactions. +We strive to maintain a welcoming and inclusive community. Please read and respect our [Code of Conduct](https://github.com/DhanushNehru/Python-Scripts/blob/main/CODE_OF_CONDUCT.md) in all your interactions. diff --git a/CSV_TO_NDJSON/README.md b/CSV_TO_NDJSON/README.md new file mode 100644 index 0000000..4aaebdb --- /dev/null +++ b/CSV_TO_NDJSON/README.md @@ -0,0 +1,38 @@ +# README.md + +# CSV to NDJSON Converter + +This Python script converts a CSV file located in this folder into an NDJSON file saved in the same folder. + +## What is NDJSON? + +NDJSON (Newline Delimited JSON) is a convenient format for streaming JSON objects, where each line is a valid JSON object. +It’s widely used in data pipelines and tools such as **Google Cloud BigQuery**, **ElasticSearch**, and many other data processing platforms. + +## How to use + +1. Place your CSV file in this folder. +2. Make sure you have Python 3 installed. +3. Run the script from this folder with: + + ```bash + python csv_to_ndjson.py input.csv output.ndjson + + +#### Example +If you have a CSV file like this: + +```csv +name,age,city +Alice,30,New York +Bob,25,Los Angeles +``` + +The output NDJSON will be: + +```json +{"name":"Alice","age":"30","city":"New York"} +{"name":"Bob","age":"25","city":"Los Angeles"} +``` + +Feel free to modify or extend it as needed. diff --git a/CSV_TO_NDJSON/csv_to_ndjson.py b/CSV_TO_NDJSON/csv_to_ndjson.py new file mode 100644 index 0000000..100da9c --- /dev/null +++ b/CSV_TO_NDJSON/csv_to_ndjson.py @@ -0,0 +1,39 @@ +# csv_to_ndjson.py +import csv +import json +import sys +import os + + +def csv_to_ndjson(csv_filename, ndjson_filename): + base_dir = os.path.dirname(os.path.abspath(__file__)) + + csv_path = os.path.join(base_dir, csv_filename) + ndjson_path = os.path.join(base_dir, ndjson_filename) + + try: + with ( + open(csv_path, mode="r", encoding="utf-8") as f_csv, + open(ndjson_path, mode="w", encoding="utf-8") as f_ndjson, + ): + reader = csv.DictReader(f_csv) + for row in reader: + f_ndjson.write( + json.dumps(row, ensure_ascii=False, separators=(",", ":")) + "\n" + ) + + print(f"Successfully converted '{csv_path}' to '{ndjson_path}'") + + except FileNotFoundError: + print(f"Error: CSV file '{csv_path}' not found.") + except Exception as e: + print(f"An error occurred: {e}") + + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("Usage: python csv_to_ndjson.py input.csv output.ndjson") + else: + csv_file = sys.argv[1] + ndjson_file = sys.argv[2] + csv_to_ndjson(csv_file, ndjson_file) diff --git a/README.md b/README.md index 7e0f239..780ebb0 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ More information on contributing and the general code of conduct for discussion | Countdown Timer | [Countdown Timer](https://github.com/DhanushNehru/Python-Scripts/tree/main/Countdown%20Timer) | Displays a message when the Input time elapses. | | Crop Images | [Crop Images](https://github.com/DhanushNehru/Python-Scripts/tree/main/Crop%20Images) | A Python script to crop a given image. | | CSV to Excel | [CSV to Excel](https://github.com/DhanushNehru/Python-Scripts/tree/main/CSV%20to%20Excel) | A Python script to convert a CSV to an Excel file. | +| CSV_TO_NDJSON | [CSV to Excel](https://github.com/DhanushNehru/Python-Scripts/tree/main/CSV_TO_NDJSON) | A Python script to convert a CSV to an NDJSON files file. | | Currency Script | [Currency Script](https://github.com/DhanushNehru/Python-Scripts/tree/main/Currency%20Script) | A Python script to convert the currency of one country to that of another. | | Digital Clock | [Digital Clock](https://github.com/DhanushNehru/Python-Scripts/tree/main/Digital%20Clock) | A Python script to preview a digital clock in the terminal. | | Display Popup Window | [Display Popup Window](https://github.com/DhanushNehru/Python-Scripts/tree/main/Display%20Popup%20Window) | A Python script to preview a GUI interface to the user. |