如何在Python中将datetime.datetime对象列表转换为日期? [重复]

时间:2023-01-14 18:38:34

This question already has an answer here:

这个问题在这里已有答案:

I am using Python whois API called 'pythonwhois' and trying to extract the 'creation_date' for a list of domain names. The code I am using is:

我正在使用名为'pythonwhois'的Python whois API,并试图提取'creation_date'以获取域名列表。我使用的代码是:

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

The result is a list of datetime.datetime objects as below:

结果是datetime.datetime对象列表,如下所示:

domain,creation_date
('hostzi.com', [datetime.datetime(2009, 5, 12, 13, 4, 12)])
('daduru.com',  [datetime.datetime(2007, 4, 16, 10, 59)])
('callforest.com', [datetime.datetime(2006, 4, 23, 14, 29, 1)])

and I want to convert the 'creation_date' column to a python the string representation of the date in the format of Y/m/d. Can anybody help?

我想将'creation_date'列转换为python日期的字符串表示形式,格式为Y / m / d。有人可以帮忙吗?

2 个解决方案

#1


2  

You can use strftime :

你可以使用strftime:

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'

Also you can do it directly on your main code :

您也可以直接在主代码上执行此操作:

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[0].strftime('%Y/%m/%d'))

        except:
            pass

#2


-2  

To convert datetime.datetime objects to datetime.date objects: https://docs.python.org/2/library/datetime.html#datetime.datetime.date

要将datetime.datetime对象转换为datetime.date对象:https://docs.python.org/2/library/datetime.html#datetime.datetime.date

EDIT: To convert datetime.datetime objects to strings in the format Y\m\d:

编辑:将datetime.datetime对象转换为Y \ m \ d格式的字符串:

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

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

#1


2  

You can use strftime :

你可以使用strftime:

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'

Also you can do it directly on your main code :

您也可以直接在主代码上执行此操作:

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[0].strftime('%Y/%m/%d'))

        except:
            pass

#2


-2  

To convert datetime.datetime objects to datetime.date objects: https://docs.python.org/2/library/datetime.html#datetime.datetime.date

要将datetime.datetime对象转换为datetime.date对象:https://docs.python.org/2/library/datetime.html#datetime.datetime.date

EDIT: To convert datetime.datetime objects to strings in the format Y\m\d:

编辑:将datetime.datetime对象转换为Y \ m \ d格式的字符串:

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

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