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

['BBBBB', '20190213', 'SUCCESS', 'Job_name1', '2019-02-14 18:11:55', '2019-02-14 18:11:56', '0.02']    

['AAAAA', '20190213', 'SUCCESS', 'job_name2', '2019-02-14 18:11:48', '2019-02-14 18:11:50', '0.03']

['AAAA', '20190213', 'WAITING', 'job_name4', 'None', 'None', '0'] 

我有一个像上面这样的数据库结果列表,记录 4,5 在 UTC 时区并且是字符串


def save_jobs_to_file(jobs_info, filename):

        '''save jobs to file

        saved to file in re-arranged order: 1 - 0 - 3 - 3 - 4 - 5 - calculated run time

        '''

        from datetime import datetime

        import pytz

        tz = pytz.timezone('America/Los_Angeles')

        utc = pytz.utc

        logging.warning('Saving jobs to file {0}...'.format(filename))

        f = open(filename, "w+")

        for line in jobs_info:

            #print(line)

            for i in range(len(line)):

                if line[i] == None:

                    pass

                else:

                    #print(datetime.strptime(line[4],'%Y-%m-%d %H:%M:%S'))

                    print(type(line[4]),type(line[5]))

                    line[4] = datetime.strptime(line[4],'%Y-%m-%d %H:%M:%S')

                    line[4] = utc.localize(line[4])

                    line[4] = line[4].strftime('%Y-%m-%d %H:%M:%S')

                    line[5] = datetime.strptime(line[5],'%Y-%m-%d %H:%M:%S')

                    line[5] = utc.localize(line[5])

                    line[5] = line[5].strftime('%Y-%m-%d %H:%M:%S')

            line = "|".join(line)

            f.write(line)

            f.write('\n')

        logging.warning('Completed saving jobs to file {0}!'.format(filename))

我正在尝试使用 strptime 函数将 4,5 条记录更改为 datetime 对象,并再次将它们转换为 pst 时区 dattime 对象并使用 strftime() 再次将它们转换为字符串。


此外,我的列表在 4,5 条记录中有 None 值,尽管我正在检查 None 值,但 strptime 仍然抛出以下错误。


我是 Python 新手,很高兴我让它成为它的一部分,但我无法弄清楚我做错了什么,尽管我将一个字符串传递给 strptime 并检查无值。


呼唤远方
浏览 333回答 2
2回答

绝地无双

   import pytz   for line in jobs_info:        #print(line)        for i in range(len(line)):            if line[i] == None:                pass            else:                #print(datetime.strptime(line[4],'%Y-%m-%d %H:%M:%S'))                print(type(line[4]),type(line[5]))                try:                    line[4] = datetime.strptime(line[4],'%Y-%m-%d %H:%M:%S')                    line[4] = pytz.utc.localize(line[4])                    line[4] = line[4].strftime('%Y-%m-%d %H:%M:%S')                    line[5] = datetime.strptime(line[5],'%Y-%m-%d %H:%M:%S')                    line[5] = pytz.utc.localize(line[5])                    line[5] = line[5].strftime('%Y-%m-%d %H:%M:%S')                except:                    pass        line = "|".join(line)        print (line)结果:BBBBB|20190213|成功|工作名称1|2019-02-14 18:11:55|2019-02-14 18:11:56|0.02 AAAAA|20190213|成功|工作名称2|2019-02-14|2019-02-14 2019-02-14 18:11:50|0.03 AAAA|20190213|WAITING|job_name4|None|None|0

MM们

尝试将“pass”替换为“continue”,“pass”只是一个占位符,因此“else”之前的表达式仍会被执行
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python