使用 python 方法 string.replace() 将星期几的名称替换为自定义字符串(例如:monday = day1/firstday/montag)

发布于 2025-01-11 21:50:20 字数 830 浏览 2 评论 0原文

我刚刚开始学习 python,正在开发一个个人迷你项目,其中我想用尽可能少的代码打印某个字符串,具体取决于用户输入的日期。 (例如:星期一 = 第 1 天/第一天/蒙塔格)

import datetime
from datetime import datetime
import calendar

my_answer = input("Enter date: (yyyy-mm-dd) ")
my_date = datetime.strptime(my_answer, "%Y-%m-%d")

year, week_num, day_of_week = my_date.isocalendar()
day_name = calendar.day_name[my_date.weekday()]

new_day = day_name.replace("Monday", "Day 1")
#new_day = day_name.replace("Tuesday", "Day 2")
# new_day = day_name.replace("Wednesday", "Day 3")
# new_day = day_name.replace("Thursday", "Day 4")
# new_day = day_name.replace("Friday", "Day 5")
# new_day = day_name.replace("Saturday", "Day 6")
# new_day = day_name.replace("Sunday", "Day 7")

print( "The date " + str(my_answer) + " is " + new_day + " of the week " + str(week_num))

I just started learning python and I'm working on a personal mini project in which I want to print with as little code as possible a certain string depending on the date the user enters.
(ex: monday = day1/firstday/montag)

import datetime
from datetime import datetime
import calendar

my_answer = input("Enter date: (yyyy-mm-dd) ")
my_date = datetime.strptime(my_answer, "%Y-%m-%d")

year, week_num, day_of_week = my_date.isocalendar()
day_name = calendar.day_name[my_date.weekday()]

new_day = day_name.replace("Monday", "Day 1")
#new_day = day_name.replace("Tuesday", "Day 2")
# new_day = day_name.replace("Wednesday", "Day 3")
# new_day = day_name.replace("Thursday", "Day 4")
# new_day = day_name.replace("Friday", "Day 5")
# new_day = day_name.replace("Saturday", "Day 6")
# new_day = day_name.replace("Sunday", "Day 7")

print( "The date " + str(my_answer) + " is " + new_day + " of the week " + str(week_num))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

小嗷兮 2025-01-18 21:50:20

您需要 re.sub

import re

string = 'Monday aaj montag nahi kal tha usko day1 ya firstdaybhi bolsakte hain'
new_string = re.sub(r'day1|firstday|montag', 'Monday', string)
print(new_string)

r'day1|firstday|montag' 只是一个正则表达式。您可以在这里阅读更多相关信息 https://docs.python.org/3/library /re.html

You need re.sub

import re

string = 'Monday aaj montag nahi kal tha usko day1 ya firstdaybhi bolsakte hain'
new_string = re.sub(r'day1|firstday|montag', 'Monday', string)
print(new_string)

r'day1|firstday|montag' is just a regular expression. You can read more about it here https://docs.python.org/3/library/re.html

你的背包 2025-01-18 21:50:20

在您的代码中,没有太多需要更改的地方。但是,我强烈建议您使用 dateutil 对象来解析输入日期:

from dateutil.parser import parse
# Rest of the code
my_date = parse(input("Enter date: (yyyy-mm-dd) ")) 
# Rest of the code

parse 是一个自动将日期字符串重新格式化为日期对象的函数。这意味着,您不需要定义日期模式(“%Y-%m-%d”)。另外,如果您输入像 My date is as the following: 1998-07-08 这样的字符串,它会智能地找到日期(“1998-07-08”)。

In your code, there is not much to change. But, I highly suggest you use dateutil object in order to parse the input date:

from dateutil.parser import parse
# Rest of the code
my_date = parse(input("Enter date: (yyyy-mm-dd) ")) 
# Rest of the code

parse is a function that automatically reformats date-strings into date objects. Meaning, you do not need to define the date patter ("%Y-%m-%d"). Also, if you put a string like My date is as what follows: 1998-07-08, it finds the date ("1998-07-08") intelligently.

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