1

This error is generated:

ValueError: time data '11/1/13' does not match format '%m/%d/%Y'

An example of my data. (Format- Month,Day,Year-value)

12/27/13,311
12/28/13,283
12/29/13,285
12/30/13,285
12/31/13,252
1/1/14,245
1/2/14,245
1/3/14,245
1/4/14,245
1/5/14,246

The code I have, collected on another SOF post.

import csv
import datetime as dt
import matplotlib.pyplot as plt

arch = 'test.csv'
data = csv.reader(open(arch))

data = [(dt.datetime.strptime(item, "%m/%d/%Y"), float(value)) for item, value in data]
data.sort()
[x, y] = zip(*data)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x, y)

ax.grid(True)
fig.autofmt_xdate()

plt.show()

Why is my date format throwing an error?

4

1 回答 1

3

你要

dt.datetime.strptime(item, "%m/%d/%y")

docs中,有几个世纪的年份(1999、2000 等)被格式化为'%Y'. 对于没有世纪的年份(对于个位数年份填充 0,例如 09、99、13),您想要使用'%y'(小写 y)。

于 2014-03-27T18:16:48.863 回答