Odoo12:strptime无法识别格式%p(PM / AM)?

问题描述 投票:0回答:1

我在模型函数中有此代码:

data_ordine = order_dict['date'].replace(" UTC","").rstrip()        
_logger.info('Simone --> DATA ORDINE : %s',data_ordine + "|")        
if data_ordine: 
 data_finale = datetime.strptime(data_ordine,"%m/%d/%Y %I:%M%p")

并且当我尝试执行它时,我收到此错误:

File "/usr/lib/python3.5/_strptime.py", line 346, in _strptime    data_string[found.end():])ValueError: unconverted data remains: PM

在日志中,我保存了传递给strptime函数的数据,我看到它是:

2020-01-21 11:12:36,221 24082 INFO db_test odoo.addons.tepp_ept.models.sale_order: Simone --> DATA ORDINE : 01/12/2020 10:42PM|

我已经尝试过在Python online compiler上的代码,但似乎还可以:

from datetime import datetime 
str_date ="01/12/2020 10:42PM" 
date = datetime.strptime(str_date,"%m/%d/%Y %I:%M%p") 
print(date)

我要去哪里错了?

python strptime odoo-12
1个回答
0
投票

对于可能处于相同情况的设备,如@Adan-Cortez所示,它与语言环境设置(特别是LC_TIME设置)有关。我通过暂时重置它,然后在strptime之后分配运算符值来解决它:

locale.setlocale(locale.LC_TIME, 'en_US.utf8')
if data_ordine:
   data_finale = datetime.strptime(data_ordine,"%m/%d/%Y %I:%M%p")

locale.setlocale(locale.LC_TIME, self.env.context['lang'] + '.utf8')
© www.soinside.com 2019 - 2024. All rights reserved.