Python 无法使用strtime分析datetime

Python 无法使用strtime分析datetime,python,numpy,strptime,Python,Numpy,Strptime,我正在尝试从csv加载数据,并将其显示在matplotlib散点图中,在X轴上我已格式化了datetime。以下是数据: 0,03/12/2017 21:00:00.000 +0000,4745 0,03/12/2017 22:00:00.000 +0000,3046 0,03/12/2017 23:00:00.000 +0000,2052 0,03/13/2017 00:00:00.000 +0000,1455 2,03/13/2017 00:00:00.000 +0000,2 1,03/13

我正在尝试从csv加载数据,并将其显示在matplotlib散点图中,在X轴上我已格式化了datetime。以下是数据:

0,03/12/2017 21:00:00.000 +0000,4745
0,03/12/2017 22:00:00.000 +0000,3046
0,03/12/2017 23:00:00.000 +0000,2052
0,03/13/2017 00:00:00.000 +0000,1455
2,03/13/2017 00:00:00.000 +0000,2
1,03/13/2017 00:00:00.000 +0000,2
以及我使用的Python3.4代码:

import numpy as np
import matplotlib.pyplot as plt
import datetime as dt

retries, count = np.loadtxt(open('search-results.csv', 'r'),
                  delimiter=",",
                  skiprows=1,
                  unpack=True,
                  usecols=[0, 2])

time = np.loadtxt(open('search-results.csv', 'r'),
                  delimiter=",",
                  skiprows=1,
                  unpack=True,
                  usecols=[1],
                  dtype=str)

dates = [dt.datetime.strptime(str(d), "b'%d/%m/%Y %H:%M:%S.000 +0000'") for d in time]

plt.scatter(dates, retries, s=count)
plt.grid(which='both', color='#aaaaaa')
plt.savefig('out.png', format='png')
当我运行上述代码时,它似乎正在解析数据,直到第13天:

ValueError: time data "b'03/13/2017 00:00:00.000 +0000'" does not match format "b'%d/%m/%Y %H:%M:%S.000 +0000'"

TL;医生:

您应该从此处更改以下行:

dates = [dt.datetime.strptime(str(d), "b'%d/%m/%Y %H:%M:%S.000 +0000'") for d in time]
为此:

dates = [dt.datetime.strptime(str(d), "b'%m/%d/%Y %H:%M:%S.000 +0000'") for d in time]

您得到的错误是正确的,因为您告诉您的代码需要一个日期,格式为:
“b'%d/%m/%Y%H:%m:%S.000+0000'”
,但您正在传递一个格式如下的日期:

"b'%m/%d/%Y %H:%M:%S.000 +0000'" (interchanged month and date).
您的代码适用于前3行,因为12在一年的月份范围内,但当它在第4行变为13时,它就中断了


祝你好运:)

是的,我需要休息一下:-)看起来很好,伙计:)