【问题标题】:Converts a date from the form yyyy/mm/dd to the form month_name day, year将日期从格式 yyyy/mm/dd 转换为格式 month_name day, year
【发布时间】:2016-12-15 01:55:51
【问题描述】:
MONTHS = ['January', 'February', 'Match', 'April', 'May', 'June',
      'July', 'August', 'September', 'October', 'November', 'December']
def date_convert(s):


    y = s[:4]
    m = int(s[5:-3])
    d = s[8:]
    if m < 10:
        nm = int(m[1:])
        month_name = MONTHS[nm - 1]
    else:
        month_name = MONTHS[m - 1]

    result= ??????
    return result


s = '2000/06/24'
r = date_convert(s)
print(r)

我正在研究期末考试视图(python 3.0 +),我整天都在练习。我突然不知道如何把 month_name a , y 作为一个字符串使用 'result' (结果=???)。然后将其返回到主程序。 这是我需要的结果:将 2000/06/24 转换为 2000 年 6 月 24 日。我的大脑现在无法工作,请有人帮助我。太感谢了。我不能使用任何内置功能。请帮我把任何东西放在一起作为一个字符串。再次感谢。

【问题讨论】:

  • 你的意思是字符串格式?您已经有了月份名称、年份和日期。您需要做的就是将它们组合成一个字符串。快速谷歌指向python-course.eu/python3_formatted_output.php之类的教程。
  • 另外,mnm字符串,所以 m &lt; 10 会失败。您需要先转换为整数,顺便说一句,这将使整个测试变得多余,因为如果您正确转换为整数,则无需截断 0
  • 谁能告诉我什么结果等于好吗?加拿大现在快凌晨三点了。我想完成它,明天我将参加期末考试。非常感谢。

标签: python


【解决方案1】:

通常您不会在 Python 中手动解析/重新格式化/转换日期。 有许多漂亮的功能甚至模块。 问题是日期转换过程虽然看起来很简单 复杂且容易出错。这就是为什么它不是每次都重新实现,而是在尽可能多的细节中实现 在 Python 核心模块中(首先在 datetime 中)。

使用strptime解析日期,使用strftime格式化日期:

>>> import datetime
>>> s = datetime.datetime.strptime("2000/06/24", "%Y/%m/%d")
>>> print s.strftime('%B %d %Y')

这两个函数都支持%Y%m等特殊格式字符:

%Y  Year with century as a decimal number.
%m  Month as a decimal number [01,12].
%d  Day of the month as a decimal number [01,31].
%B  Locale’s full month name.

您可以在此处找到更多信息:

另外,请检查这个问题,它与您的问题非常接近 更多答案:

如果是练习, 你必须实现转换功能 自己一个人,也可以。您的实现几乎是正确的 但你错过了一些要点:

def date_convert(s):
    "Manual date conversion"

    y = s[:4]
    m = int(s[5:-3])
    d = s[8:]
    month_name = MONTHS[m - 1]
    return "%s %s, %s" % (month_name, d, y) 

您的函数中不需要if。 而且您不需要两次 int 转换,只需要第一个。

你可以写得更简洁:

def date_convert(s):
    "Manual date conversion"

    return "%s %s, %s" % (MONTHS[int(s[5:-3])-1], s[8:], s[:4]) 

【讨论】:

    【解决方案2】:
    MONTHS = ['January', 'February', 'Match', 'April', 'May', 'June',
          'July', 'August', 'September', 'October', 'November', 'December']
    def date_convert(s):
        y = s[:4]
        m = int(s[5:-3])
        d = s[8:]
        month_name = MONTHS[m-1]
    
    result= month_name + ' ' + d + ' '  + y
    return result
    s = '2000/06/24'
    r = date_convert(s)
    print r
    

    我在 python 2 中只是更改了打印功能

    【讨论】:

      【解决方案3】:

      试试这个:

      from datetime import datetime
      
      d = "2000/06/24"
      date_obj = datetime.strptime(d, "%Y/%m/%d")
      formatted_date = datetime.strftime(date_obj, "%B %d %Y")
      print(formatted_date)
      

      因此,您可以将 d 作为变量作为参数传递给您正在使用的函数。

      strptime(): is used to convert the string of the date to a date object.
      strftime(): is used to convert the date object to string.
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-05
        • 1970-01-01
        • 2022-01-25
        • 2017-06-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-31
        相关资源
        最近更新 更多