Reputation: 287
I am trying to convert the string that contains hours, minutes and seconds to datetime from a csv file but always get error "ValueError: time data 'created_at' does not match format '%Y/%m/%d %H:%M:%S'". The code is as below :
import csv
from dateutil.parser import parse
import pandas as pd
import time
from datetime import datetime
path = r'C:\Users\Ahmed Ismail Khalid\Desktop\test\_jonasschnelli__tweets.csv'
with open(path,'rt',encoding="utf-8") as f :
reader = csv.reader(f)
for row in reader :
print(row[1])
print("Converted time is :", datetime.strptime(row[1], '%d/%m/%Y %H:%M:%S'))
The csv file is in the format :
id | created_at | text
1 | 1/15/2018 6:12:16 AM | this is sample text
2 | 1/11/2018 6:58:27 AM | this is sample text
3 | 1/10/2018 9:39:33 PM | this is sample text
4 | 1/10/2018 8:47:35 PM | this is sample text
Any help would be appreciated.
Thanks
Upvotes: 0
Views: 334
Reputation: 1012
You should skip the first row of your csv file which is:
id | created_at | text
Also, your sample date and the format dont match.
The following block code should work if your date format is:
Day/Month/Year Hour:Minute:Second Locale
for idx, row in enumerate(ints):
if idx != 1:
print("Converted time is :", datetime.strptime('1/11/2018 6:30:23 PM', '%d/%m/%Y %H:%M:%S %p'))
Also, make sure to take a look at strptime documentation.
Upvotes: 1