-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_csv_file_delimerer.py
35 lines (28 loc) · 1.16 KB
/
update_csv_file_delimerer.py
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
import csv
def convert_csv(input_file: str, output_file: str) -> None:
"""
Convert a CSV file with specific headers to a new CSV file with specified field names.
Args:
input_file (str): Path to the input CSV file.
output_file (str): Path to the output CSV file.
Returns:
None
"""
fieldnames = ['id', 'class_index', 'title', 'description']
with open(input_file, 'r', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
with open(output_file, 'w', newline='', encoding='utf-8') as newcsvfile:
writer = csv.DictWriter(newcsvfile, fieldnames=fieldnames, delimiter=';')
writer.writeheader()
for idx, row in enumerate(reader, start=1):
new_row = {
'id': str(idx),
'class_index': row['Class Index'],
'title': row['Title'],
'description': row['Description']
}
writer.writerow(new_row)
# Usage example
input_file = 'excel_files/train.csv'
output_file = 'excel_files/train_fixed.csv'
convert_csv(input_file, output_file)