python:词典列表中的格式dateTime字符串

发布于 2025-02-10 01:17:52 字数 1025 浏览 3 评论 0原文

我目前有以下内容,在该以下,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

薯片软お妹 2025-02-17 01:17:52
from datetime import datetime

rec_list = {
    "AllergiesList": [
        {
            "Date": "2021-09-03T00:00:00",
            "Description": "string",
            "Source": "string",
            "Severity": "string",
            "Reaction": "string",
            "TypeCode": "string",
            "Notes": "string",
            "Notes1": "string",
            "TenancyDescription": "string"
        },
        {
            "Date": "2021-09-09T00:00:00",
            "Description": "string",
            "Source": "string",
            "Severity": "string",
            "Reaction": "string",
            "TypeCode": "string",
            "Notes": "string",
            "Notes1": "string",
            "TenancyDescription": "string"
        }
    ],
    "TotalItemCount": 0
}

for rec in rec_list["AllergiesList"]:
    rec["Date"] = datetime.strptime(rec["Date"], "%Y-%m-%dT%H:%M:%S").strftime("%d/%m/%Y")

print(rec_list)

使用上述代码段,您可以根据要求将所有日期格式记录转换。

输出: -

{
    "AllergiesList": [
        {
            "Date": "03/09/2021",
            "Description": "string",
            "Source": "string",
            "Severity": "string",
            "Reaction": "string",
            "TypeCode": "string",
            "Notes": "string",
            "Notes1": "string",
            "TenancyDescription": "string",
        },
        {
            "Date": "09/09/2021",
            "Description": "string",
            "Source": "string",
            "Severity": "string",
            "Reaction": "string",
            "TypeCode": "string",
            "Notes": "string",
            "Notes1": "string",
            "TenancyDescription": "string",
        },
    ],
    "TotalItemCount": 0,
}

谢谢:)

from datetime import datetime

rec_list = {
    "AllergiesList": [
        {
            "Date": "2021-09-03T00:00:00",
            "Description": "string",
            "Source": "string",
            "Severity": "string",
            "Reaction": "string",
            "TypeCode": "string",
            "Notes": "string",
            "Notes1": "string",
            "TenancyDescription": "string"
        },
        {
            "Date": "2021-09-09T00:00:00",
            "Description": "string",
            "Source": "string",
            "Severity": "string",
            "Reaction": "string",
            "TypeCode": "string",
            "Notes": "string",
            "Notes1": "string",
            "TenancyDescription": "string"
        }
    ],
    "TotalItemCount": 0
}

for rec in rec_list["AllergiesList"]:
    rec["Date"] = datetime.strptime(rec["Date"], "%Y-%m-%dT%H:%M:%S").strftime("%d/%m/%Y")

print(rec_list)

Using above code snippet you can convert your all date format records as per your requirment.

Output:-

{
    "AllergiesList": [
        {
            "Date": "03/09/2021",
            "Description": "string",
            "Source": "string",
            "Severity": "string",
            "Reaction": "string",
            "TypeCode": "string",
            "Notes": "string",
            "Notes1": "string",
            "TenancyDescription": "string",
        },
        {
            "Date": "09/09/2021",
            "Description": "string",
            "Source": "string",
            "Severity": "string",
            "Reaction": "string",
            "TypeCode": "string",
            "Notes": "string",
            "Notes1": "string",
            "TenancyDescription": "string",
        },
    ],
    "TotalItemCount": 0,
}

Thanks :)

爱情眠于流年 2025-02-17 01:17:52

只需替换is_ recrecent函数,然后以下一个

import datetime 

def is_recent(entry, amount):
    limit_time = datetime.datetime.now() - datetime.timedelta(months=amount)
    return datetime.datetime.strptime(entry["Date"], '%Y-%m-%dT%H:%M:%S') > limit_time

just replace is_recent function with following one

import datetime 

def is_recent(entry, amount):
    limit_time = datetime.datetime.now() - datetime.timedelta(months=amount)
    return datetime.datetime.strptime(entry["Date"], '%Y-%m-%dT%H:%M:%S') > limit_time
猫九 2025-02-17 01:17:52

尝试下面的代码,

from datetime import datetime

date_time_str = entry['Date']
date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%dT%H:%M:%S')
date = date_time_obj.strftime("%d/%m/%Y")
print('Date String:', date)

如果需要DD/MM/YY格式,它将给定格式转换为DD/MM/YYYY,%y in in in y date_time_obj.strftime(“%d/%m/%y”)

Try the code below, it will convert the given format to DD/MM/YYYY

from datetime import datetime

date_time_str = entry['Date']
date_time_obj = datetime.strptime(date_time_str, '%Y-%m-%dT%H:%M:%S')
date = date_time_obj.strftime("%d/%m/%Y")
print('Date String:', date)

If you want DD/MM/YY format replace %Y with %y in date_time_obj.strftime("%d/%m/%Y")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文