简体   繁体   English

从 python 中的字符串中提取日期、月份和年份

[英]extract date, month and year from string in python

I have this column where the string has date, month, year and also time information.我有这个列,其中字符串包含日期、月份、年份和时间信息。 I need to take the date, month and year only.我只需要获取日期、月份和年份。

There is no space in the string.字符串中没有空格。

The string is on this format:该字符串采用以下格式:

date
Tuesday,August22022-03:30PMWIB
Monday,July252022-09:33PMWIB
Friday,January82022-09:33PMWIB

and I expect to get:我希望得到:

date
2022-08-02
2022-07-25
2022-01-08

How can I get the date, month and year only and change the format into yyyy-mm-dd in python?如何仅获取日期、月份和年份并将格式更改为 python 中yyyy-mm-dd

thanks in advance提前致谢

You can use the standard datetime library您可以使用标准的datetime

from datetime import datetime

dates = [
    "Tuesday,August22022-03:30PMWIB",
    "Monday,July252022-09:33PMWIB",
    "Friday,January82022-09:33PMWIB"
]

for text in dates:
    text = text.split(",")[1].split("-")[0]
    dt = datetime.strptime(text, '%B%d%Y')
    print(dt.strftime("%Y-%m-%d"))

An alternative/shorter way would be like this (if you want the other date parts):另一种/更短的方法是这样的(如果你想要其他日期部分):

for text in dates:
    dt = datetime.strptime(text[:-3], '%A,%B%d%Y-%I:%M%p')
    print(dt.strftime("%Y-%m-%d"))

The timezone part is tricky and works only for UTC, GMT and local.时区部分很棘手,仅适用于 UTC、GMT 和本地。 You can read more about the format codes here .您可以在此处阅读有关格式代码的更多信息。

strptime() only accepts certain values for %Z: strptime() 只接受 %Z 的某些值:

any value in time.tzname for your machine's locale time.tzname 中机器语言环境的任何值

the hard-coded values UTC and GMT硬编码值 UTC 和 GMT

Use strptime from datetime library使用日期时间库中的 strptime

var = "Tuesday,August22022-03:30PMWIB"
date = var.split('-')[0]
formatted_date = datetime.strptime(date, "%A,%B%d%Y")
print(formatted_date.date()) #this will get your output

Output: Output:

2022-08-02

You can convert to datetime object then get string back.您可以转换为日期时间 object 然后取回字符串。

from datetime import datetime
datetime_object = datetime.strptime('Tuesday,August22022-03:30PM', '%A,%B%d%Y-%I:%M%p')
s = datetime_object.strftime("%Y-%m-%d")
print(s)

You can use the datetime library to parse the date and print it in your format.您可以使用datetime库来解析日期并以您的格式打印。 In your examples the day might not be zero padded so I added that and then parsed the date.在您的示例中,这day可能不是零填充的,所以我添加了它,然后解析了日期。

import datetime

date = 'Tuesday,August22022-03:30PMWIB'
date = date.split('-')[0]

if not date[-6].isnumeric():
    date = date[:-5] + "0" + date[-5:]
    newdate = datetime.datetime.strptime(date, '%A,%B%d%Y').strftime('%Y-%m-%d')
    print(newdate)
    # prints 2022-08-02

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM