iT邦幫忙

0

python 英文月份取代數字月份

  • 分享至 

  • xImage

c=input("請輸入年/月/日:")
n=c.find("/")
l=c[n+4:]
g=c[5:7]
f=c[:n]

month = ("Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec.")

c=l+"/"+g+"/"+f
print(c)

要怎麼讓g(自行輸入的月份)被字串month對應的取代
程式超級小白求解

ccutmis iT邦高手 2 級 ‧ 2023-03-14 18:41:38 檢舉
可以用字典,例如:
month = {"01":"Jan." , "02":"Feb." , "03":"Mar." , "04":"Apr." , "05":"May" , "06":"Jun." , "07":"Jul." , "08":"Aug." , "09":"Sep." , "10":"Oct." , "11":"Nov." , "12":"Dec."}
print(month[g]) #假如 g 為 '03' 就會印出 'Mar.'
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
2
黃彥儒
iT邦高手 1 級 ‧ 2023-03-14 18:46:31

我覺得,我是不是應該要嗆你,連ChatGPT都不問了嗎?/images/emoticon/emoticon67.gif
https://ithelp.ithome.com.tw/upload/images/20230314/20088395ohyDaQwfaz.png
https://ithelp.ithome.com.tw/upload/images/20230314/20088395QfTLqcby23.png

0
obarisk
iT邦研究生 2 級 ‧ 2023-03-15 07:05:18

%b

0
alien663
iT邦研究生 5 級 ‧ 2023-03-15 08:54:01
def get_month(month_int):
    month = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.", "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
    return month[month_int-1]

c=input("請輸入年/月/日:")
p = c.split("/")

print(p[0] + "/" + get_month(int(p[1])) + "/" + p[2])

0
sam0407
iT邦大師 1 級 ‧ 2023-03-16 15:30:18

Python初學者來練功囉!!
像這種日期格式轉換的,我一向不自己寫,儘量用現成的....

#iT邦幫忙練功
#https://ithelp.ithome.com.tw/questions/10212374

#原PO程式碼
c=input("請輸入年/月/日:")
n=c.find("/")
l=c[n+4:]
g=c[5:7]
f=c[:n]

#我的程式碼
import datetime
dt1 = datetime.date(year = int(f), month = int(g), day=int(l))
c = dt1.strftime("%Y/%b/%d")
print(c)

執行結果:
https://ithelp.ithome.com.tw/upload/images/20230316/20012665U42TCih4BO.png

我要發表回答

立即登入回答