Python ValueError: 时间数据与格式“%Y-%m-%d %H:%M:%S.%f”不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41862525/
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-08-20 01:46:11  来源:igfitidea点击:

ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f'

pythondatetime

提问by roy

I am facing one little problem. I am storing some date time data and the data is

我正面临一个小问题。我正在存储一些日期时间数据,数据是

# "datetime","numb","temperature"

"1998-04-18 16:48:36.76",0,38
"1998-04-18 16:48:36.8",1,42
"1998-04-18 16:48:36.88",2,23
"1998-04-18 16:48:36.92",3,24
"1998-04-18 16:48:36",4,42
"1998-04-18 16:48:37",5,33
"1998-04-18 16:48:37.08",6,25

the date time column is clearly string, so when I try to convert it , I got this error

日期时间列显然是字符串,所以当我尝试转换它时,我收到了这个错误

ValueError: time data '1998-04-18 16:48:36' does not match format '%Y-%m-%d %H:%M:
%S.%f'

my code is

我的代码是

import time
import datetime
import calendar

for k, line in enumerate(lines):
                if k > (int(header_line)):
                    data_pre = line.strip().split(',')
                    stDate = data_pre[0].replace("\"", "")
                    print stDate  # got 1998-04-18 16:48:36.76


                    dat_time = datetime.datetime.strptime(stDate,
                                                       '%Y-%m-%d %H:%M:%S.%f')
                    mic_sec = dat_time.microsecond
                    timcon = calendar.timegm(dat_time.timetuple())*1000000 + mic_sec
                    strDate = "\"" + strDate + "\""
                    print stDate # got "1998-04-18 16:48:36.76"

because some of my datetime column is missing .%f value, so I got this error. my documents might contains a few thousands such date time values, so I came up with solution to append .0 with all such date time. so that if date time string is

因为我的一些日期时间列缺少 .%f 值,所以我收到了这个错误。我的文档可能包含几千个这样的日期时间值,所以我想出了将所有这些日期时间附加到 .0 的解决方案。所以如果日期时间字符串是

"1998-04-18 16:48:36"

my code should append .0 to fulfill the format criteria. e.g

我的代码应该附加 .0 以满足格式标准。例如

"1998-04-18 16:48:36.0"

I try to append .0 to stDate, but I get this error

我尝试将 .0 附加到 stDate,但出现此错误

AttributeError: 'str' object has no attribute 'append'

If somebody gives me a clue how to deal with such a problem. Any help would be greatly appreciated.

如果有人给我一个线索如何处理这样的问题。任何帮助将不胜感激。

回答by Chuck

Update: I've looked through your code and found some misstypes. In addition, it looks like you didn't add in the concatenation.

更新:我查看了您的代码并发现了一些错误类型。此外,您似乎没有添加连接。

I have sorted both out.

我已经把两者都整理好了。

Mistypes:

错误类型:

You wrote:

你写了:

for k, line in enumerate(lines):
                if k > (int(header_line)):
                    data_pre = line.strip().split(',')
                    stDate = data_pre[0].replace("\"", "")
                    print stDate  # got 1998-04-18 16:48:36.76


                    dat_time = datetime.datetime.strptime(stDate,
                                                   '%Y-%m-%d %H:%M:%S.%f')
                    mic_sec = dat_time.microsecond
                    timcon = calendar.timegm(dat_time.timetuple())*1000000 + mic_sec

                    strDate = "\"" + strDate + "\""
                    # ^ This line is wrong
                    # It should say: 
                    # strDate = "\"" + stDate + "\""

                    print stDate # got "1998-04-18 16:48:36.76"
                    # ^ This line is wrong
                    # It should say:
                    # print strDate

Implementing the above changes, we can now add the " + ".0" " addition to a sample of your code

实现上述更改,我们现在可以将“+”.0“”添加到您的代码示例中

(Try running this first, make sure you understand what it is doing, before moving on):

(先尝试运行它,确保在继续之前了解它在做什么):

import time
import datetime
import calendar

A = "1998-04-18 16:48:36.76,0,38"
B = "1998-04-18 16:48:37,5,33"

# Run the Code for B

data_pre = B.strip().split(',')
print data_pre

stDate = data_pre[0].replace("\"", "")
print "stDate before: ", stDate  

### Addition of Addition of .0
# Here, we try to convert to datetime format using the format
# '%Y-%m-%d %H:%M:%S.%f'
try:
    dat_time = datetime.datetime.strptime(stDate,
                               '%Y-%m-%d %H:%M:%S.%f')

# If that doesn't work, we add ".4" to the end of stDate
# (You can change this to ".0")
# We then retry to convert stDate into datetime format                                   
except:
    stDate = stDate + ".4"
    dat_time = datetime.datetime.strptime(stDate,
                               '%Y-%m-%d %H:%M:%S.%f')
    print "stDate after: ", stDate

###                                
print "dat_time: ", dat_time

mic_sec = dat_time.microsecond
print "mic_sec: ", mic_sec

timcon = calendar.timegm(dat_time.timetuple())*1000000 + mic_sec
print "timecon: ", timcon

strDate = "\"" + stDate + "\""
print "strDate: ", strDate 

Therefore, for an example:

因此,例如:

A = "1998-04-18 16:48:36.76,0,38"
B = "1998-04-18 16:48:37,5,33"
# Note the difference  ^^

# Output for B:
['1998-04-18 16:48:37', '5', '33']
stDate before:  1998-04-18 16:48:37
stDate after:  1998-04-18 16:48:37.4
dat_time:  1998-04-18 16:48:37.400000
mic_sec:  400000
timecon:  892918117400000
strDate:  "1998-04-18 16:48:37.4"

# Output for A:
['1998-04-18 16:48:36.76', '0', '38']
stDate before:  1998-04-18 16:48:36.76
dat_time:  1998-04-18 16:48:36.760000
mic_sec:  760000
timecon:  892918116760000
strDate:  "1998-04-18 16:48:36.76"

Integrated Everything into your main loop. This is what you want overall:

将所有内容集成到您的主循环中。这是您总体上想要的:

for k, line in enumerate(lines):
                if k > (int(header_line)):
                    data_pre = line.strip().split(',')
                    stDate = data_pre[0].replace("\"", "")
                    print stDate  

                    try:
                        dat_time = datetime.datetime.strptime(stDate,
                               '%Y-%m-%d %H:%M:%S.%f')                                  
                    except:
                        stDate = stDate + ".4"
                        dat_time = datetime.datetime.strptime(stDate,
                               '%Y-%m-%d %H:%M:%S.%f')

                    mic_sec = dat_time.microsecond
                    timcon = calendar.timegm(dat_time.timetuple())*1000000 + mic_sec

                    strDate = "\"" + stDate + "\""
                    # ^ Changed this line
                    print strDate 
                    # ^ Changed this line

Original Answer:

原答案:

You can't append to a string.

您不能附加到字符串。

One option would be to use A + B

一种选择是使用 A + B

A = "1998-04-18 16:48:36"
B = ".0"
C = A + B
C = "1998-04-18 16:48:36.0"

You can also use str.join:

您还可以使用str.join

D = "".join([A,B])
D = '1998-04-18 16:48:36.0'

For more info, see the answer to this question: Which is the preferred way to concatenate a string in Python?

有关更多信息,请参阅此问题的答案:在 Python 中连接字符串的首选方法是哪种?

回答by Vishwas Abhyankar

Instead of formatting datetime with strfunction, try datetime.datetime.strftimefunction:

而不是用str函数格式化日期时间,试试datetime.datetime.strftime函数:

Code that does not work:

不起作用的代码:

>>> import datetime
>>> import pytz
>>> jst = pytz.timezone('Asia/Tokyo')
>>> dt = jst.localize(datetime.datetime.now())
>>> dt
datetime.datetime(2018, 10, 11, 14, 42, 28, 557170, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)
>>> str(dt)
'2018-10-11 14:42:28.557170+09:00'
>>> dt_new = datetime.datetime.strptime(str(dt), '%Y-%m-%d %H:%M:%S.%f%z')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 565, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_strptime.py", line 362, in _strptime
    (data_string, format))
ValueError: time data '2018-10-11 14:42:28.557170+09:00' does not match format '%Y-%m-%d %H:%M:%S.%f%z'

Code that works:

有效的代码:

>>> import datetime
>>> import pytz
>>> jst = pytz.timezone('Asia/Tokyo')
>>> dt = jst.localize(datetime.datetime.now())
>>> dt
datetime.datetime(2018, 10, 11, 14, 42, 28, 557170, tzinfo=<DstTzInfo 'Asia/Tokyo' JST+9:00:00 STD>)
>>> dt.strftime('%Y-%m-%d %H:%M:%S.%f%z')
'2018-10-11 14:42:28.557170+0900'
>>> dt_new = datetime.datetime.strptime(dt.strftime('%Y-%m-%d %H:%M:%S.%f%z'), '%Y-%m-%d %H:%M:%S.%f%z')
>>> dt_new
datetime.datetime(2018, 10, 11, 14, 42, 28, 557170, 
tzinfo=datetime.timezone(datetime.timedelta(0, 32400)))

So here I formatted the datetime with strftime function and parsed the formatted datetime using strptime function keeping formatter same in both cases.

所以在这里我使用 strftime 函数格式化日期时间并使用 strptime 函数解析格式化的日期时间,在两种情况下保持格式化程序相同。

There is no way to parse the datetime having timezone information and formatted using str(datetime)function.

无法解析具有时区信息并使用str(datetime)函数格式化的日期时间。