import csv import json import argparse def convert_tsv_to_jsonl(input_tsv_path): # Generate the output file name by replacing .tsv with .jsonl output_jsonl_path = input_tsv_path.rsplit('.', 1)[0] + '.jsonl' # Open the input TSV file and the output JSON lines file with open(input_tsv_path, mode='r', encoding='utf-8') as tsv_file, \ open(output_jsonl_path, mode='w', encoding='utf-8') as jsonl_file: # Create a CSV reader object to read the TSV file tsv_reader = csv.reader(tsv_file, delimiter='\t') # Iterate over each row in the TSV file for row in tsv_reader: # Each row should have exactly two columns: label and text if len(row) == 2: label, text = row # Create a dictionary to represent this record record = {'label': label, 'text': text} # 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 TSV file to JSONL file.") # Add argument for input TSV file parser.add_argument("input_tsv_path", type=str, help="Path to the input TSV file.") # Parse arguments args = parser.parse_args() # Convert TSV to JSONL using the provided input path convert_tsv_to_jsonl(args.input_tsv_path) if __name__ == "__main__": main()