File size: 1,736 Bytes
4e81ff1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import csv
import json
import argparse

def convert_csv_to_jsonl(input_csv_path):
    # Generate the output file name by replacing .csv with .jsonl
    output_jsonl_path = input_csv_path.rsplit('.', 1)[0] + '.jsonl'

    # Open the input CSV file and the output JSON lines file
    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:

        # Create a CSV reader object to read the CSV file
        csv_reader = csv.reader(csv_file, delimiter=',')
        
        # Iterate over each row in the CSV file
        for row in csv_reader:
            # Each row should have exactly four columns: label, text, full_label, date
            if len(row) == 4:
                label, text, full_label, date = row
                # Create a dictionary to represent this record
                record = {
                    'label': label,
                    'text': text,
                    'full_label': full_label,
                    'date': date
                }
                # Convert the dictionary to a JSON string and write to the JSON lines file
                jsonl_file.write(json.dumps(record) + '\n')
            else:
                print(f"Skipping malformed row: {row}")

def main():
    # Create argument parser
    parser = argparse.ArgumentParser(description="Convert CSV file to JSONL file.")
    # Add argument for input CSV file
    parser.add_argument("input_csv_path", type=str, help="Path to the input CSV file.")
    
    # Parse arguments
    args = parser.parse_args()

    # Convert CSV to JSONL using the provided input path
    convert_csv_to_jsonl(args.input_csv_path)

if __name__ == "__main__":
    main()