|
import csv |
|
import json |
|
import argparse |
|
|
|
def convert_tsv_to_jsonl(input_tsv_path): |
|
|
|
output_jsonl_path = input_tsv_path.rsplit('.', 1)[0] + '.jsonl' |
|
|
|
|
|
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: |
|
|
|
|
|
tsv_reader = csv.reader(tsv_file, delimiter='\t') |
|
|
|
|
|
for row in tsv_reader: |
|
|
|
if len(row) == 2: |
|
label, text = row |
|
|
|
record = {'label': label, 'text': text} |
|
|
|
jsonl_file.write(json.dumps(record) + '\n') |
|
else: |
|
print(f"Skipping malformed row: {row}") |
|
|
|
def main(): |
|
|
|
parser = argparse.ArgumentParser(description="Convert TSV file to JSONL file.") |
|
|
|
parser.add_argument("input_tsv_path", type=str, help="Path to the input TSV file.") |
|
|
|
|
|
args = parser.parse_args() |
|
|
|
|
|
convert_tsv_to_jsonl(args.input_tsv_path) |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|
|
|