打印格式化只有一个索引位置的元组
Print formatting a tuple that only has one index position
my_tuple = tuple([(user,date)] for user,date in tuple_search if datetime.now() > date - timedelta(days=60))
message_text = ('You are receiving this message because your account is going to expire on {}. Please log in to reset your password.'.format(my_tuple))
print(message_text)
>>>You are receiving this message because your account is going to expire on ([(<User username:dev>, datetime.datetime(2020, 12, 10, 20, 3, 32))], [(<User username:lol>, datetime.datetime(2021, 1, 21, 17, 3, 25))]. Please log in to reset your password.
我想格式化上面的元组,以便在每个打印语句中它只打印开括号中的日期。像 is going to expire on (12/10/2020)
这样的东西,它还会打印出下一条具有不同日期的消息,如 is going to expire on (1/21/2021)
我在将元组拆分为不同元素时遇到问题。它目前只有一个元素,我不确定如何将其格式化为多个元素。
要从日期时间中提取日期并设置日期格式,您可以使用 f 字符串和日期时间对象的参数。
for elem in my_tuple:
print(f"The expiry date for Username {elem[0][0]}\n")
print(f" is ({elem[0][1].month}/{elem[0][1].day}/{elem[0][1].year})")
它产生(在我的 my_tuple
变量的编造版本中。如果你能给我们一个可复制的版本,我很乐意编辑):
The date for user1
is (12/1/2020)
The date for user2
is (12/1/2020)
f-strings 是 3.6 中添加的一个很好的功能,它使事情更容易阅读。
my_tuple = tuple([(user,date)] for user,date in tuple_search if datetime.now() > date - timedelta(days=60))
message_text = ('You are receiving this message because your account is going to expire on {}. Please log in to reset your password.'.format(my_tuple))
print(message_text)
>>>You are receiving this message because your account is going to expire on ([(<User username:dev>, datetime.datetime(2020, 12, 10, 20, 3, 32))], [(<User username:lol>, datetime.datetime(2021, 1, 21, 17, 3, 25))]. Please log in to reset your password.
我想格式化上面的元组,以便在每个打印语句中它只打印开括号中的日期。像 is going to expire on (12/10/2020)
这样的东西,它还会打印出下一条具有不同日期的消息,如 is going to expire on (1/21/2021)
我在将元组拆分为不同元素时遇到问题。它目前只有一个元素,我不确定如何将其格式化为多个元素。
要从日期时间中提取日期并设置日期格式,您可以使用 f 字符串和日期时间对象的参数。
for elem in my_tuple:
print(f"The expiry date for Username {elem[0][0]}\n")
print(f" is ({elem[0][1].month}/{elem[0][1].day}/{elem[0][1].year})")
它产生(在我的 my_tuple
变量的编造版本中。如果你能给我们一个可复制的版本,我很乐意编辑):
The date for user1
is (12/1/2020)
The date for user2
is (12/1/2020)
f-strings 是 3.6 中添加的一个很好的功能,它使事情更容易阅读。