0

我是 Python 的新手。我试图玩时间和日期对象。我编写了一个简单的测试程序来将字符串解析为特定的时间格式。但它抛出一个ValueError. 你能帮帮我吗?

这是代码:

import time
testDate = "Tuesday, Febuary 23 2011 12:00:00 UTC"
today = time.strptime(testDate,"%A, %B %d %Y %H:%M:%S %Z")
print today

和错误:

Traceback (most recent call last):
  File "D:\Python\PythonTest\src\helloWorld.py", line 3, in <module>
    today = time.strptime(testDate,"%A, %B %d %Y %H:%M:%S %Z")
  File "C:\Python27\Lib\_strptime.py", line 467, in _strptime_time
    return _strptime(data_string, format)[0]
  File "C:\Python27\Lib\_strptime.py", line 325, in _strptime
   (data_string, format))
ValueError: time data 'Tuesday, Febuary 23 2011 12:00:00 UTC' does not match format '%A, %B %d %Y %H:%M:%S %Z'
4

2 回答 2

3

你把二月拼错了。您的代码有效。

import time
testDate = "Tuesday, February 23 2011 12:00:00 UTC"
today = time.strptime(testDate,"%A, %B %d %Y %H:%M:%S %Z")
print today
于 2013-08-26T15:25:07.423 回答
1

很简单:你拼错了“二月”:

import time
testDate = "Tuesday, February 23 2011 12:00:00 UTC"
today = time.strptime(testDate,"%A, %B %d %Y %H:%M:%S %Z")
print today

最初,您将“二月”拼写为“二月”。修复后工作正常。

于 2013-08-26T15:25:00.480 回答