【问题标题】:ValueError: time data 'End Date' does not match format '%m/%d/%Y'ValueError:时间数据“结束日期”与格式“%m/%d/%Y”不匹配
【发布时间】:2020-05-03 18:43:32
【问题描述】:

我正在尝试将日期转换为时间戳

我正在从 csv 及其内容中读取数据

CID,End Date,License Key 
bcc1,3/11/2022,abc

我的主要功能是这样的

    with open(sys.argv[1],'r') as file:
        csvfile= csv.reader(file,delimiter=",")
        for row in csvfile:
            CID = row[0]
            DATE = str(row[1])
            END_DATE = datetime.datetime.strptime(DATE,"%m/%d/%Y").timetuple()
            # END_DATE= datetime.datetime(int(END_DATE)).strftime('%s')
            EVAL_SKU_LIST = row[2]
            print CID
            print (DATE)
            print EVAL_SKU_LIST

我收到一个错误提示

ValueError:时间数据“结束日期”与格式“%m/%d/%Y”不匹配

【问题讨论】:

  • DATE 的值是多少?
  • 2022 年 3 月 11 日在上述 CSV 中
  • 跳过第一行。代码试图解释标题行值?
  • 是的,就是这样。

标签: python csv


【解决方案1】:
CID、结束日期、许可证密钥 bcc1,3/11/2022,abc

ValueError:时间数据“结束日期”与格式“%m/%d/%Y”不匹配

这一切都在错误消息中。您将字符串 'End Date' 传递给 strptime,而不是 '3/11/2022'

您需要跳过包含标题的 CSV 文件的第一行。

例如,像这样:

csvfile = csv.reader(file, delimiter=",")
headers = next(csvfile)  # read one row
for row in csvfile:      # iterate over all subsequent rows
    # ...

【讨论】:

  • 有没有办法区分标题和内容?
猜你喜欢
  • 2022-01-20
  • 2018-03-18
  • 1970-01-01
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
相关资源
最近更新 更多