The error message 'ValueError: time data '2020/1/1' doesn't match format '%Y-%m-%d %H:%M:%S'' is raised by Python's datetime.strptime() function when the input string doesn't match the specified format. In your case, the input string '2020/1/1' is not in the expected format '%Y-%m-%d %H:%M:%S', which requires dates in the format 'YYYY-MM-DD HH:MM:SS'.

Here are the solutions to fix this error:

  1. Use the correct format string: If your input string is in the format 'YYYY/MM/DD', change the format string to '%Y/%m/%d':
datetime.strptime('2020/1/1', '%Y/%m/%d')
  1. Use the 'ISO8601' argument: If your input strings are in the ISO8601 format, but not necessarily in the same format, use the 'ISO8601' argument:
datetime.fromisoformat('2020/1/1')
  1. Use the 'mixed' argument: For mixed format input strings, use the 'mixed' argument. The format will be inferred individually. You can also use the 'dayfirst' argument alongside this:
datetime.strptime('2020/1/1', format='mixed', dayfirst=True)

Choose the solution that suits your data and update your code accordingly to resolve the 'ValueError' you're encountering.

Python datetime.strptime() Error: 'ValueError: time data '2020/1/1' doesn't match format '%Y-%m-%d %H:%M:%S'' - Solution Guide

原文地址: https://www.cveoy.top/t/topic/nCPr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录