python:词典列表中的格式dateTime字符串
我目前有以下内容,在该以下,ALERGIESLIST可以是一个或多个dicts
。
{
"AllergiesList": [
{
"Date": "2021-09-03T00:00:00",
"Description": "string",
"Source": "string",
"Severity": "string",
"Reaction": "string",
"TypeCode": "string",
"Notes": "string",
"Notes1": "string",
"TenancyDescription": "string"
}
],
"TotalItemCount": 0
}
我的问题是,我将如何格式化所有字典中的date
键,以特定格式,例如d/m/y
我当前有以下内容来删除dicts
超过X个月大的
def is_recent(entry,amount):
limit_time = pd.Timestamp.today() - relativedelta.relativedelta(months=amount)
return pd.to_datetime(entry["Date"]) > limit_time
使用如下:
allergies = session["allergies"] # This is the same as the code snippet above i.e. a list of dictionaries
session["allergies"] = [entry for entry in allergies["AllergiesList"] if is_recent(entry,6)]
我需要做另一个def()
,还是可以修改我的当前一个以做需要的事情?
I currently have the following where the AllergiesList can be one or more dicts
.
{
"AllergiesList": [
{
"Date": "2021-09-03T00:00:00",
"Description": "string",
"Source": "string",
"Severity": "string",
"Reaction": "string",
"TypeCode": "string",
"Notes": "string",
"Notes1": "string",
"TenancyDescription": "string"
}
],
"TotalItemCount": 0
}
My question is how would I format the Date
key within all dictionaries to be in a specific format e.g. d/m/Y
I currently have the following to remove dicts
that are more than X months old
def is_recent(entry,amount):
limit_time = pd.Timestamp.today() - relativedelta.relativedelta(months=amount)
return pd.to_datetime(entry["Date"]) > limit_time
Used as follows:
allergies = session["allergies"] # This is the same as the code snippet above i.e. a list of dictionaries
session["allergies"] = [entry for entry in allergies["AllergiesList"] if is_recent(entry,6)]
Would I need to do another def()
or can my current one be amended to do what's needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用上述代码段,您可以根据要求将所有日期格式记录转换。
输出: -
谢谢:)
Using above code snippet you can convert your all date format records as per your requirment.
Output:-
Thanks :)
只需替换is_ recrecent函数,然后以下一个
just replace is_recent function with following one
尝试下面的代码,
如果需要DD/MM/YY格式,它将给定格式转换为DD/MM/YYYY,
%y
inin
in
date_time_obj.strftime(“%d/%m/%y”)y
Try the code below, it will convert the given format to DD/MM/YYYY
If you want DD/MM/YY format replace
%Y
with%y
indate_time_obj.strftime("%d/%m/%Y")