ValueError:时间数据与格式'%Y-%m-%d%H:%M:%S.%f'不匹配 [英] ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f'

查看:2070
本文介绍了ValueError:时间数据与格式'%Y-%m-%d%H:%M:%S.%f'不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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'

我的代码是

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"

因为我的某些datetime列缺少。%f值,所以出现了此错误,我的文档中可能包含数千个这样的日期时间值,所以我想出了追加解决方案。 0和所有这样的日期时间。因此,如果日期时间字符串为

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

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

我的代码应附加.0以符合格式标准。例如

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

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

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

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

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.

推荐答案

更新:我仔细阅读了您的代码,发现了一些错误类型。
此外,您似乎没有添加串联。

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

我都把它们都解决了。

您写道:

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



实现从头开始更改后,我们现在可以在代码示例中添加 + .0



(尝试先运行此代码,确保您了解它是什么)

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

(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"



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

对于k,

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.

一种选择是使用 A + B

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

您还可以使用 str.join

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

有关更多信息,请参见以下问题的答案:在Python中连接字符串的首选方法是什么?

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

这篇关于ValueError:时间数据与格式'%Y-%m-%d%H:%M:%S.%f'不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆