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

时间:2019-02-14 23:37:30

标签: python sql datetime

['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仍然抛出以下错误。

(<type 'str'>, <type 'str'>)
Traceback (most recent call last):
  File "./heart_latest_dev_get_pc_jobs.py", line 239, in <module>
    main_program()
  File "./heart_latest_dev_get_pc_jobs.py", line 222, in main_program
    save_jobs_to_file(enhanced_jobs,filename)
  File "./heart_latest_dev_get_pc_jobs.py", line 104, in save_jobs_to_file
    line[4] = datetime.strptime(line[4],'%Y-%m-%d %H:%M:%S')
  File "/usr/lib64/python2.7/_strptime.py", line 332, in _strptime
    (data_string, format))
ValueError: time data 'None' does not match format '%Y-%m-%d %H:%M:%S'

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

按照@John Anderson的建议

我希望我已经正确实现了他的建议,但是我仍然遇到相同的错误:

Edit(1):

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:
        #if line[0] != None:
        for i in range(len(line)):
            if  line[0] != None:
                #print(datetime.strptime(line[4],'%Y-%m-%d %H:%M:%S'))
                #print(line[0])
                #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))

编辑(1)后出现错误:

   Traceback (most recent call last):
      File "./heart_latest_dev_get_pc_jobs.py", line 240, in <module>
        main_program()
      File "./heart_latest_dev_get_pc_jobs.py", line 223, in main_program
        save_jobs_to_file(enhanced_jobs,filename)
      File "./heart_latest_dev_get_pc_jobs.py", line 103, in save_jobs_to_file
        line[4] = datetime.strptime(line[4],'%Y-%m-%d %H:%M:%S')
      File "/usr/lib64/python2.7/_strptime.py", line 332, in _strptime
        (data_string, format))
    ValueError: time data 'None' does not match format '%Y-%m-%d %H:%M:%S'



 *****Any help would be much appriciated :)*****

2 个答案:

答案 0 :(得分:0)

   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 | SUCCESS | job_name2 | 2019-02-14 18:11:48 | 2019-02-14 18:11:50 | 0.03 AAAA | 20190213 |等待中| job_name4 |无|无| 0

答案 1 :(得分:0)

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