无法使用strptime解析日期时间

时间:2017-04-10 09:03:46

标签: python numpy strptime

我正在尝试从csv加载数据并将其显示在matplotlib散点图中,其中在X轴上我已经格式化了日期时间。这是数据:

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'"

1 个答案:

答案 0 :(得分:1)

<强> TL; DR:

您应该更改以下行:

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时就会中断!

祝你好运:)