为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

呼唤远方 2021-11-09 16:18:11
['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 并检查无值。
查看完整描述

2 回答

?
绝地无双

TA贡献1946条经验 获得超4个赞

   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


查看完整回答
反对 回复 2021-11-09
?
MM们

TA贡献1886条经验 获得超2个赞

尝试将“pass”替换为“continue”,“pass”只是一个占位符,因此“else”之前的表达式仍会被执行


查看完整回答
反对 回复 2021-11-09
  • 2 回答
  • 0 关注
  • 308 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信