Skip to content Skip to sidebar Skip to footer

Inserting Date Correctly Into SQL

I have Python code like this: with open('task9_in.csv', 'r') as file: reader = csv.reader(file, delimiter = ',') for row in reader: all_data.append(row) The data s

Solution 1:

Given that the CSV file looks like this:

test1, 1/6/2019, 1243, login

You are missing the padding for date in your CSV file itself. So, you can covert the date string from your row using datetime library.

This library is flexible enough to support multiple formatting. More info here (https://thispointer.com/python-how-to-convert-datetime-object-to-string-using-datetime-strftime/)

Try this:

import datetime

before_string = '1/6/2019'

date = list(map(int, before_string.split('/'))

date_obj = datetime.date(date[2], date[1], date[0])

padded_string = date_obj.strftime('%d/%m/%Y')

print(padded_string)        

Outputs:

01/06/2019

Now this can then be inserted into your SQL table.


Post a Comment for "Inserting Date Correctly Into SQL"