Skip to content
Advertisement

Allow two datetime format using raise ValueError python

I have two csv files with two different format which are '%m/%d/%Y %H:%M:%S' and '%d/%m/%Y %H:%M:%S'. Hence, I will get such ValueError :

JavaScript

Sample data from file1.csv where month is in the middle:

JavaScript

While file2.csv, month is at front :

JavaScript

The column comes as String then I require to transform to only extract out the date. This is the code :

JavaScript

My plan is to redirect format= to a function that will overcome ValueError and change the format from '%m/%d/%Y %H:%M:%S' to '%d/%m/%Y %H:%M:%S'.

Something like this.. :

JavaScript

Advertisement

Answer

Try with

JavaScript

Then

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement