如何强制 pythons 格式方法在评估时放置值
How to force pythons format method to place values as they are evaluated
我需要根据日期计算出会计年度,当我将格式方法与 datetime.datetime 对象一起使用时,它会为具有不同值的相同类型对象生成意外结果
下面是我的代码。
from datetime import datetime
dt = datetime.strptime('2019-03-03','%Y-%m-%d')
## Below line is killing my mind as it is resulting 2019-2018
print('{}-{}'.format(dt.year, (dt.year+1)%100 if dt.month > 3 else dt.year-1,(dt.year)%100))
# This will produce the result 3 2019 19
print(dt.month, dt.year, (dt.year)%100)
dt = datetime.strptime('2019-04-04','%Y-%m-%d')
# But the below line is working fine as it is resulting 2019-20
print('{}-{}'.format(dt.year, (dt.year+1)%100 if dt.month > 3 else dt.year-1,(dt.year)%100))
# This will produce the result 4 2019 19
print(dt.month, dt.year, (dt.year)%100)
我期待结果
2018-19 if dt = datetime.strptime('2019-03-03','%Y-%m-%d')
2019-20 if dt = datetime.strptime('2019-04-04','%Y-%m-%d')
我无法找出代码的问题。
## Below line is killing my mind as it is resulting 2019-2018
print('{}-{}'.format(dt.year, (dt.year+1)%100 if dt.month > 3 else dt.year-1,(dt.year)%100))
好的,让我们分解一下您的代码:
'{}-{}'.format(dt.year, (dt.year+1)%100 if dt.month > 3 else dt.year-1,(dt.year)%100)
你有 2 个 {}
,但有 3(!) 个参数:
dt.year
,
(dt.year+1)%100 if dt.month > 3 else dt.year-1
,
(dt.year)%100
(忽略因为只有2个{}
)
如您所见,if/else
仅适用于中间参数。
你想要的是在两个参数上都使用这个 if
,所以你要么需要重复 if 要么使用括号来分组。但是分组会产生一个元组,所以你需要用 *
解包值(我在评论中提到了分组,但忘了解包)。
具有 2 个 ifs 的解决方案:
'{}-{}'.format(dt.year if dt.month > 3 else dt.year-1,
(dt.year+1)%100 if dt.month > 3 else (dt.year)%100)
如您所见,一个逗号 - 两个参数。为了便于阅读,将其分为两行。
一个 if 和元组解包的解决方案:
'{}-{}'.format( *(dt.year, (dt.year+1)%100) if dt.month > 3 else *(dt.year-1,(dt.year)%100) )
为什么要拆包?因为 '{}-{}'.format( ('2018','19') )
得到一个元组参数,而不是两个参数。它不知道如何处理它。前面的 *
解压列表或元组并将它们作为普通参数提供。 - 阅读更多相关信息 here in the documentation。
我需要根据日期计算出会计年度,当我将格式方法与 datetime.datetime 对象一起使用时,它会为具有不同值的相同类型对象生成意外结果
下面是我的代码。
from datetime import datetime
dt = datetime.strptime('2019-03-03','%Y-%m-%d')
## Below line is killing my mind as it is resulting 2019-2018
print('{}-{}'.format(dt.year, (dt.year+1)%100 if dt.month > 3 else dt.year-1,(dt.year)%100))
# This will produce the result 3 2019 19
print(dt.month, dt.year, (dt.year)%100)
dt = datetime.strptime('2019-04-04','%Y-%m-%d')
# But the below line is working fine as it is resulting 2019-20
print('{}-{}'.format(dt.year, (dt.year+1)%100 if dt.month > 3 else dt.year-1,(dt.year)%100))
# This will produce the result 4 2019 19
print(dt.month, dt.year, (dt.year)%100)
我期待结果
2018-19 if dt = datetime.strptime('2019-03-03','%Y-%m-%d')
2019-20 if dt = datetime.strptime('2019-04-04','%Y-%m-%d')
我无法找出代码的问题。
## Below line is killing my mind as it is resulting 2019-2018
print('{}-{}'.format(dt.year, (dt.year+1)%100 if dt.month > 3 else dt.year-1,(dt.year)%100))
好的,让我们分解一下您的代码:
'{}-{}'.format(dt.year, (dt.year+1)%100 if dt.month > 3 else dt.year-1,(dt.year)%100)
你有 2 个 {}
,但有 3(!) 个参数:
dt.year
,(dt.year+1)%100 if dt.month > 3 else dt.year-1
,(dt.year)%100
(忽略因为只有2个{}
)
如您所见,if/else
仅适用于中间参数。
你想要的是在两个参数上都使用这个 if
,所以你要么需要重复 if 要么使用括号来分组。但是分组会产生一个元组,所以你需要用 *
解包值(我在评论中提到了分组,但忘了解包)。
具有 2 个 ifs 的解决方案:
'{}-{}'.format(dt.year if dt.month > 3 else dt.year-1,
(dt.year+1)%100 if dt.month > 3 else (dt.year)%100)
如您所见,一个逗号 - 两个参数。为了便于阅读,将其分为两行。
一个 if 和元组解包的解决方案:
'{}-{}'.format( *(dt.year, (dt.year+1)%100) if dt.month > 3 else *(dt.year-1,(dt.year)%100) )
为什么要拆包?因为 '{}-{}'.format( ('2018','19') )
得到一个元组参数,而不是两个参数。它不知道如何处理它。前面的 *
解压列表或元组并将它们作为普通参数提供。 - 阅读更多相关信息 here in the documentation。