File size: 1,565 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 |
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()
|