获取时间数据“”与格式“%Y-%m-%d %H:%M:%S.%f”不匹配
Getting time data '' does not match format '%Y-%m-%d %H:%M:%S.%f'
我有一个日期时间错误,但只针对某些行。这是我的代码:
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)
如果我 运行 直到第 232 行,一切正常,但如果我 运行 多于此,我会收到错误消息。只解析没有时间数据转换的字符串我可以得到结果,但我不知道这是否可以,或者我可能遗漏了什么。这是字符串代码:
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)
许多行不包含日期字段。在这些情况下 Date
为空,因此您的 .strptime()
调用将失败。我建议您跳过这些行,例如:
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('orders.csv', 'wb') as f:
f.write(r.content)
import_data_files()
path = 'orders.csv'
orders_placed = 0
orders_date = []
with open(path, 'r') as fl:
csv = csv.reader(fl, delimiter=',', quotechar='"')
next(csv)
for row_number, (CustomerID, OrderID, Date, OrderTotal, ProductName, Price) in enumerate(csv):
if Date:
date = datetime.datetime.strptime(Date, '%Y-%m-%d %H:%M:%S.%f')
if date.year == 2021 and date.month == 10:
orders_placed += 1
print(orders_placed)
这将显示:437
通常一行看起来像:
1900610,7692429,2020-01-12 13:52:40.677,200.0000,0981,200.0000
但是没有日期的失败行看起来像:
5014,5365,,120.0000,211,60.0000
我有一个日期时间错误,但只针对某些行。这是我的代码:
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)
如果我 运行 直到第 232 行,一切正常,但如果我 运行 多于此,我会收到错误消息。只解析没有时间数据转换的字符串我可以得到结果,但我不知道这是否可以,或者我可能遗漏了什么。这是字符串代码:
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)
许多行不包含日期字段。在这些情况下 Date
为空,因此您的 .strptime()
调用将失败。我建议您跳过这些行,例如:
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('orders.csv', 'wb') as f:
f.write(r.content)
import_data_files()
path = 'orders.csv'
orders_placed = 0
orders_date = []
with open(path, 'r') as fl:
csv = csv.reader(fl, delimiter=',', quotechar='"')
next(csv)
for row_number, (CustomerID, OrderID, Date, OrderTotal, ProductName, Price) in enumerate(csv):
if Date:
date = datetime.datetime.strptime(Date, '%Y-%m-%d %H:%M:%S.%f')
if date.year == 2021 and date.month == 10:
orders_placed += 1
print(orders_placed)
这将显示:437
通常一行看起来像:
1900610,7692429,2020-01-12 13:52:40.677,200.0000,0981,200.0000
但是没有日期的失败行看起来像:
5014,5365,,120.0000,211,60.0000