python 使用 datetime.strptime 时对“ValueError: time data ...不匹配格式”进行故障排除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2460233/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-04 00:42:25  来源:igfitidea点击:

Troubleshooting 'ValueError: time data ... does not match format' when using datetime.strptime

pythonstrptime

提问by Rahul99

My input string is '16-MAR-2010 03:37:04'and i want to store it as datetime.

我的输入字符串是'16-MAR-2010 03:37:04',我想将其存储为日期时间。

I am trying to use:

我正在尝试使用:

db_inst.HB_Create_Ship_Date = datetime.strptime(fields[7]," %d-%b-%Y %H:%M:%S ") 
fields[7] = '16-MAR-2010 03:37:04' 

I am getting an error:

我收到一个错误:

::ValueError: time data '16-MAR-2010 03:37:04' does not match format ' %d-%b-%Y %H:%M:%S ' 

回答by mechanical_meat

Edit:
As John mentions, make it easier on yourself and remove the leading and trailing spaces.

编辑:
正如约翰提到的,让自己更轻松并删除前导和尾随空格。

Another thought:
Your current localemay not specify "MAR" as a month abbreviation.

另一个想法:
您当前的语言环境可能没有将“MAR”指定为月份的缩写。

What does the output of this code give?:

这段代码的输出给出了什么?:

import locale
locale.getdefaultlocale()

I tested your code on a Linux machine (Ubuntu 9.10, Python 2.6.4) and got the ValueError.
I removed the spaces, changed to non-English locale (Czech), and got the ValueError.

我在 Linux 机器(Ubuntu 9.10、Python 2.6.4)上测试了你的代码并得到了 ValueError。
我删除了空格,更改为非英语语言环境(捷克语),并得到 ValueError。

Academic note:
Oddly your code works on Windows XP Python 2.5.5 with the extraneous spaces:

学术笔记:
奇怪的是,您的代码在带有无关空间的 Windows XP Python 2.5.5 上运行:

>>> from datetime import datetime
>>> dt = '16-MAR-2010 03:37:04'
>>> datetime.strptime(dt, " %d-%b-%Y %H:%M:%S ")
datetime.datetime(2010, 3, 16, 3, 37, 4)

回答by John Machin

Lose the spaces at the front and back of your format. I thought that strptime was documented to vary depending on the whims of whoever wrote the C runtime for your box. However it seems I'm wrong. Which would mean that there's a bug in Python.

丢失格式前后的空格。我认为 strptime 被记录为根据为您的机器编写 C 运行时的人的奇思妙想而有所不同。不过好像我错了。这意味着 Python 中存在错误。

Python 2.6.4 on Windows doesn't like leading trailing spaces; see below.

Windows 上的 Python 2.6.4 不喜欢尾随空格;见下文。

*x users, what do you find?

*x 用户,你发现了什么?

In the meantime, use the lowest common denominator -- lose the spaces. You may also have a locale problem, as Adam mentioned.

同时,使用最小的公分母——去掉空格。正如 Adam 所提到的,您可能还有语言环境问题。

With spaces:

带空格:

>>> datetime.datetime.strptime('16-MAR-2010 03:37:04'," %d-%b-%Y %H:%M:%S ")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\python26\lib\_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data '16-MAR-2010 03:37:04' does not match format ' %d-%b-%Y %H
:%M:%S '

Without spaces:

没有空格:

>>> datetime.datetime.strptime('16-MAR-2010 03:37:04',"%d-%b-%Y %H:%M:%S")
datetime.datetime(2010, 3, 16, 3, 37, 4)
>>>

回答by Will

Your format string has a leading space and a trailing space, but your input string does not. Remove the space after the starting quotation mark and before the ending quotation mark.

您的格式字符串有一个前导空格和一个尾随空格,但您的输入字符串没有。删除起始引号之后和结束引号之前的空格。

回答by Kyle Rozendo

Try this:

试试这个:

db_inst.HB_Create_Ship_Date = datetime.strptime(fields[7],"%d-%b-%Y %H:%M:%S")

Also, have a look hereas a reference for DateTime formatting in Python.

此外,请查看此处作为 Python 中 DateTime 格式的参考。