2017-04-10 76 views
1

我想从csv加载数据并将其显示在matplotlib散点图中,其中X轴上的格式化日期时间。这是数据:无法使用strptime解析日期时间

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

当我运行上面的代码,它看起来就像是分析数据,直到它到达第十三天:

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

TL; DR:

,则应该更换followi NG线从这样的:

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是在一年的月份范围内,但是当它在第四行获得13时,它会中断!

祝你好运:)

+0

呀,我需要:-) – ftraian

+0

休息似乎很配合:) –