Skip to content

✨ Add CSV to NDJSON conversion script and update README ✨ #440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
38 changes: 38 additions & 0 deletions CSV_TO_NDJSON/README.md
Original file line number Diff line number Diff line change
@@ -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.
39 changes: 39 additions & 0 deletions CSV_TO_NDJSON/csv_to_ndjson.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down