Last active
May 26, 2025 11:36
-
-
Save oguzhanaslann/0205317ed7ed227a4afca6c33afa98e5 to your computer and use it in GitHub Desktop.
Simple function to convert csv file into jsonl files written in python
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_jsonl(input_csv, output_jsonl): | |
| try: | |
| # Open the CSV file | |
| with open(input_csv, 'r', encoding='utf-8') as csv_file: | |
| # Read CSV file | |
| csv_reader = csv.DictReader(csv_file) | |
| # Open the JSONL file for writing | |
| with open(output_jsonl, 'w', encoding='utf-8') as jsonl_file: | |
| # Convert each row to JSON and write to the output file | |
| for row in csv_reader: | |
| # Convert the row to JSON and write it as a line | |
| jsonl_file.write(json.dumps(row, ensure_ascii=False) + '\n') | |
| print(f"Successfully converted {input_csv} to {output_jsonl}") | |
| except Exception as e: | |
| print(f"An error occurred: {str(e)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment