'Getting time data '' does not match format '%Y-%m-%d %H:%M:%S.%f'
I'm having a datetime error but only for some rows. Here's my code:
import requests
import csv
import datetime
def import_data_files():
r = requests.get('https://raw.githubusercontent.com/anyoneai/notebooks/main/customers_and_orders/data/orders.csv')
with open('./sample_data/orders.csv', 'wb') as f:
f.write(r.content)
import_data_files()
path = './sample_data/orders.csv'
row_cnt = 0
orders_placed = 0
orders_date = []
with open(path, 'r') as fl:
csv = csv.reader(fl, delimiter=',', quotechar='"')
next(csv)
for CustomerID, OrderID, Date, OrderTotal, ProductName, Price in csv:
date = datetime.datetime.strptime(Date, '%Y-%m-%d %H:%M:%S.%f')
if date.year == 2021:
if date.month == 10:
#print(Date)
#print(date)
orders_placed+=1
row_cnt+=1
if row_cnt > 234:
#if row_cnt > 232:
break
print(orders_placed)
if I run until row 232 everything works, but if I run more than that I get an error. Parsing only the strings without timedata conversion I can get the result, but I don't know if that's ok or maybe I'm missing something. Here's the string code:
import requests
import csv
def import_data_files():
r = requests.get('https://raw.githubusercontent.com/anyoneai/notebooks/main/customers_and_orders/data/orders.csv')
with open('./sample_data/orders.csv', 'wb') as f:
f.write(r.content)
import_data_files()
path = './sample_data/orders.csv'
orders_placed = 0
with open(path, 'r') as fl:
csv = csv.reader(fl, delimiter=',', quotechar='"')
next(csv)
for CustomerID, OrderID, Date, OrderTotal, ProductName, Price in csv:
date = Date.split(" ")
date = Date.split("-")
if date[0] == '2021':
if date[1] == '10':
orders_placed+=1
print(orders_placed)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
