The input data comes from a CSV file in which dates are formatted in AM/PM and then the Pandas library is used to export the csv to a sqlite database table.
Are there any elegant solutions or a builtin function for either python 2.7 or 3 to convert these 12 hour dates to 24 hour format, preferably before they are written to the database? I need it to compare dates which will be imported to the database.
The following code is used:
import pandas as pd
import datetime as dt
from sqlite3 import connect
from glob import iglob
from os.path import splitext, basename
dbName = 'logs'
logsDir = 'event_logs'
def csvToDb(db, filename):
conn = connect(db)
df = pd.read_csv(filename,encoding='utf-8')
df.to_sql(splitext(basename(filename))[0], conn)
conn.close()
def csvLogsToDb(directory):
for filename in iglob(directory + '/*.csv'):
print('WRITING TO DB | ' + filename)
csvToDb(dbName, filename)
print("DONE")
if __name__ == "__main__":
csvLogsToDb(logsDir)
I removed most data and kept the most relevant, but in a nutshell, this is my current input and needed output
INPUT CSV DATA:
Level,DateTime
Information,2017/01/06 9:06:02 AM
Information,2017/01/06 9:02:44 AM
Information,2017/01/06 8:56:48 PM
CURRENT OUTPUT
COL1
Information
Information
Information
COL2
2017/01/06 9:06:02 AM
2017/01/06 9:02:44 AM
2017/01/06 8:56:48 PM
EXPECTED OUTPUT
COL1
Information
Information
Information
COL2
2017-01-06 09:06:02
2017-01-06 09:02:44
2017-01-06 20:56:48