import csv
from datetime import datetime
input_file = '/path/to/input/file.csv'
output_file = '/path/to/output/file.csv'
# Mapping of column names from the input format to the output format
column_mapping = {
'File name': 'File Name',
'Timestamp': 'Start Datetime [dd.mm.yyyy hh:mm]',
'Success': 'Print result [success canceled failed]',
'Print time': 'Duration',
'Spool': 'Spool Name',
'Filament length': 'Used Length [mm]',
'Filament volume': 'Used Weight [g]',
'User': 'User'
}
# Function to convert the timestamp format
def convert_timestamp(timestamp):
return datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S').strftime('%d.%m.%Y %H:%M')
# Function to convert the success status
def convert_success(success):
return 'success' if success == '1' else 'failed'
# Function to convert the duration format
def convert_duration(duration):
hours, minutes, seconds = map(int, duration.split(':'))
return f'{hours}h{minutes}m{seconds}s'
# Read the input file and convert the data
with open(input_file, 'r') as file:
reader = csv.DictReader(file)
rows = []
for row in reader:
converted_row = {}
for input_column, output_column in column_mapping.items():
if input_column == 'Timestamp':
converted_row[output_column] = convert_timestamp(row[input_column])
elif input_column == 'Success':
converted_row[output_column] = convert_success(row[input_column])
elif input_column == 'Print time':
converted_row[output_column] = convert_duration(row[input_column])
else:
converted_row[output_column] = row[input_column]
rows.append(converted_row)
# Write the converted data to the output file
with open(output_file, 'w', newline='') as file:
writer = csv.DictWriter(file, fieldnames=column_mapping.values())
writer.writeheader()
writer.writerows(rows)