I have a csv file that has multiple columns, one named 'dates'. Inside 'dates' contains some dates, and some of the rows have more dates than others. Here is an example of what the dates look like inside of the file:
dates
31-Mar-24
Nov 21, 2024, Apr 14, 2025, May 18, 2025
21-Oct-23
26-Sep-24
22-Nov-23
24-Sep-24
13-Nov-23
10-Apr-24
23-Sep-23
Apr 16, 2025, Jun 04, 2025
I would like the dates to be separated into their own individual columns. If you use my previous example, one of the rows has the data 'Nov 21, 2024, Apr 14, 2025, May 18, 2025'. I would like it to look like this in the output:
date date2: date3:
nov 1, 2024 Apr 14, 2025 you get it right
And so on, you get the point, I want all columns with more than one date to be placed into another column but it must be in the same row.
Here is the code that I have tried but it did not work:
import csv
with open('input.csv', 'r') as csv_input_file, open('output.csv', 'w', newline='') as csv_output_file:
reader = csv.reader(csv_input_file)
writer = csv.writer(csv_output_file)
header = next(reader)
new_columns = []
for column in header:
if column == 'dates':
new_columns.extend(['date' + str(i+1) for i in range(10)]) # maximum of 10 date columns
else:
new_columns.append(column)
writer.writerow(new_columns)
for row in reader:
dates_str = row[header.index('dates')]
dates_list = dates_str.split(',')
dates_list = [date.strip() for date in dates_list]
new_row = []
for column in row:
if column == dates_str:
new_row.extend(dates_list)
new_row.extend([''] * (len(new_columns) - len(new_row)))
else:
new_row.append(column)
writer.writerow(new_row)
This is my current code but the issue is that it is splitting the dates at every comma so the years are being split from the months and day into all new columns. Cant seem to find a solution for it and was hoping someone on here could help.