【问题标题】:ValueError: time data does not match format '%Y-%m-%d %H:%M:%S.%f'ValueError: 时间数据与格式 '%Y-%m-%d %H:%M:%S.%f' 不匹配
【发布时间】:2017-01-25 22:05:18
【问题描述】:

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

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

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

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"

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

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

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

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

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

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

如果有人告诉我如何处理这样的问题。任何帮助将不胜感激。

【问题讨论】:

    标签: python datetime


    【解决方案1】:

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

    我都整理好了。

    错误输入:

    你写道:

    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“”添加到您的代码示例中

    (先尝试运行它,确保您了解它在做什么,然后再继续):

    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 
    

    因此,举个例子:

    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"
    

    将 Everything 集成到您的主循环中。这就是你想要的整体:

    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
    

    原答案:

    你不能追加到一个字符串。

    一种选择是使用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'
    

    有关详细信息,请参阅此问题的答案:Which is the preferred way to concatenate a string in Python?

    【讨论】:

    • A + B = "1998-04-18 16:48:36.0" 这行可能会让人们感到困惑。我想说把它改成C = A + B #1998-04-18 16:48:36.0
    • @StamKaly 完成。好地方。
    • @CharlesMorris 和 stamkaly 感谢您的帮助。我会试一试,一会儿再来。
    • @CharlesMorris 我接受你的回答,我非常理解。它可以根据我的需要完美运行。非常感谢您的帮助和您宝贵的时间。你解释得很好很容易。祝你有美好的一天。
    • @roy 不客气。我希望你这样做:) (P.s. 最后一个考虑:在Try Except 添加。这是非常笼统的。您可能希望更改它以专门检查stDate 的格式是否符合要求(目前,它如果上一行失败,只需将.0添加到你给它的任何内容中)。如果你遇到非常不同的数据,可能不是很明显这是导致它失败的原因。也许可以尝试专门检查stDate.的格式try 部分,可能使用日期时间。有关错误的更多信息,请参阅:stackoverflow.com/questions/855759/python-try-else)
    【解决方案2】:

    假设我的数据框有两列,“日期”和“时间”。

    将字符 '-' 替换为 '/' 或您计划实施的任何字符。单独添加此行为我修复了错误。

    df.Date = df.Date.str.replace('-', '/')
    

    在此处使用日期和时间创建一个新列

    df['DateTimeStart'] = df['Date'] + '-' + df['Time']
    

    去掉所有空格以防万一。

    df.DateTimeStart = df.DateTimeStart.str.replace(' ', '')
    

    现在将列格式化为日期时间格式

    df['DateTimeStart'] = pd.to_datetime(df['DateTimeStart'], format='%d/%m/%Y-%H:%M:%S')
                     
    

    【讨论】:

      【解决方案3】:

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

      不起作用的代码:

      >>> 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'
      

      有效的代码:

      >>> 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)))
      

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

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

      【讨论】:

        猜你喜欢
        • 2016-10-15
        • 1970-01-01
        • 2017-06-04
        • 1970-01-01
        • 2022-01-20
        • 2021-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多