File size: 1,362 Bytes
f0524dd |
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 |
import csv
import json
def crop_json(file_path, num_lines, new_file_path):
with open(file_path, 'r') as file:
data = json.load(file)
cropped_data = data[:num_lines]
with open(new_file_path, 'w') as new_file:
json.dump(cropped_data, new_file)
# file_paths = [
# {'input': './data/corpus/train.json', 'size': 2000, 'output': './data/corpus-cut/train.json'},
# {'input': './data/corpus/test.json', 'size': 500, 'output': './data/corpus-cut/test.json'},
# {'input': './data/corpus/val.json', 'size': 500, 'output': './data/corpus-cut/val.json'}
# ]
#
# for file_path in file_paths:
# crop_json(file_path['input'], file_path['size'], file_path['output'])
def crop_csv(file_path, num_lines, new_file_path):
with open(file_path, 'r') as file:
reader = csv.reader(file)
data = [row for row in reader]
cropped_data = data[:num_lines]
with open(new_file_path, 'w') as new_file:
writer = csv.writer(new_file)
writer.writerows(cropped_data)
file_paths = [
{'input': './train.csv', 'size': 2000, 'output': './train.csv'},
{'input': './test.csv', 'size': 500, 'output': './test.csv'},
{'input': './validation.csv', 'size': 500, 'output': './validation.csv'}
]
for file_path in file_paths:
crop_csv(file_path['input'], file_path['size'], file_path['output'])
|