如何转换日期时间。日期时间对象到目前为止在Python中?

2024-05-16 18:51:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的pythonwhoisapi名为“pythonwhois”,并试图提取域名列表的“创建日期”。我使用的代码是:

f = open (file,'r')
with open (output,'wt') as m:
    for line in f:
        line = line.strip('\n')
        domain = line.split(';')
        try:
            w = pythonwhois.get_whois(domain)
            c_date = (w['creation_date'])
            print (domain,c_date)

        except:
            pass

结果是日期时间。日期时间对象如下:

^{pr2}$

我想把'creation_date'列转换成python,它是日期的字符串表示,格式为Y/m/d。 有人能帮忙吗?在


Tags: 代码列表outputdatedomainwithline时间
2条回答

您可以使用^{}

Return a string representing the date and time, controlled by an explicit format string:

>>> l=('hostzi.com', [datetime.datetime(2009, 5, 12, 13, 4, 12)])
>>> l[1][0].strftime('%Y/%m/%d')
'2009/05/12'

也可以直接在主代码上执行:

^{pr2}$

转换日期时间。日期时间对象日期时间.日期物体: https://docs.python.org/2/library/datetime.html#datetime.datetime.date

编辑: 转换日期时间。日期时间对象到格式为Y\m\d的字符串:

d = datetime.datetime.now()
d.strftime("%Y\%m\%d")

https://docs.python.org/2/library/datetime.html#datetime.datetime.strftime

相关问题 更多 >