![](/img/trans.png)
[英]ValueError: time data '140120 1520' does not match format '%Y-%m-%d %H:%M:%S'
[英]ValueError: time data 'None' does not match format '%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 仍然抛出以下错误。
(<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 并检查无值。
正如@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 :)*****
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 | SUCCESS |工作名称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
尝试将“pass”替换为“continue”,“pass”只是一个占位符,因此“else”之前的表达式仍会被执行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.