|
import csv |
|
import json |
|
import argparse |
|
|
|
def convert_csv_to_jsonl(input_csv_path): |
|
|
|
output_jsonl_path = input_csv_path.rsplit('.', 1)[0] + '.jsonl' |
|
|
|
|
|
with open(input_csv_path, mode='r', encoding='utf-8') as csv_file, \ |
|
open(output_jsonl_path, mode='w', encoding='utf-8') as jsonl_file: |
|
|
|
|
|
csv_reader = csv.reader(csv_file, delimiter=',') |
|
|
|
|
|
for row in csv_reader: |
|
|
|
if len(row) == 4: |
|
label, text, full_label, date = row |
|
|
|
record = { |
|
'label': label, |
|
'text': text, |
|
'full_label': full_label, |
|
'date': date |
|
} |
|
|
|
jsonl_file.write(json.dumps(record) + '\n') |
|
else: |
|
print(f"Skipping malformed row: {row}") |
|
|
|
def main(): |
|
|
|
parser = argparse.ArgumentParser(description="Convert CSV file to JSONL file.") |
|
|
|
parser.add_argument("input_csv_path", type=str, help="Path to the input CSV file.") |
|
|
|
|
|
args = parser.parse_args() |
|
|
|
|
|
convert_csv_to_jsonl(args.input_csv_path) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|
|
|