ValueError:时间数据'12 / 16/2016'与格式'%d /%m /%Y'不匹配

时间:2018-02-09 23:56:14

标签: python python-3.x csv

我已导入CSV,而我正在尝试在周末销售的CSV文件中打印产品,但出于某种原因我无法将日历日期转换为工作日?

Look = open("ShopRecords.csv", "r")
#Store The Content Of The File In A ReadCSV.
ReadFile = list(csv.reader(Look))
ReadFile.pop(0)
print(ReadFile)
#Loop Through Each Line In The Record.
for Record in ReadFile:
    DateConvert = datetime.datetime.strptime(Record[2], "%d/%m/%Y")
    print(DateConvert)
#If The Line Contains Saturday, Print The Item Name, Which Is The Second Item.
    if Sat in Record:
        print(Record[1])
#If The Line Contains Sunday, Print The Item Name, Just Like If It's Saturday.
    if Sun in Record:
        print(Record[1])

1 个答案:

答案 0 :(得分:3)

%m是月份说明符, 16 不能是月份号码,因此必须 12 16 应该是日期编号(%d)。所以,你需要格式:

%m/%d/%Y

匹配 2016年12月16日