1

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:

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:

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?

UserYmY
  • 8,034
  • 17
  • 57
  • 71
  • _I want to convert the 'creation_date' column to a python date object in the format of Y/m/d._ - are you sure you want a date object, or a string representation of the date? They are two very different things. – Burhan Khalid Jan 11 '15 at 10:30
  • @Burhan Khalid You are right my mistake. what I want is the string representation of the date – UserYmY Jan 11 '15 at 10:31

2 Answers2

3

You can use 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
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • So, I wasn't sure if he wanted a string, but there is a `datetime.datetime` object and a `datetime.date` object. I interpreted the question as he wants to convert it to a date object... However, I am not 100% sure about this. – Dair Jan 11 '15 at 10:29
  • Nah, it appears he has mentioned I was wrong in the comments. – Dair Jan 11 '15 at 10:32
  • Thanks Kasra and Bair. Not that it is very important but I am not a 'he';) – UserYmY Jan 11 '15 at 10:35
  • @Mee: parentheses around `w['creattion_date']` do nothing here. Do not use bare `except`. It may catch too much e.g., `KeyboardInterrupt` (`SIGINT`) or `AttributeError` (a bug). – jfs Jan 11 '15 at 15:34
  • Thanks a lot for your comment@J.F. Sebastian. I did not fully understand your point about exception here. Can you elaborate more? – UserYmY Jan 12 '15 at 12:41
-2

To convert datetime.datetime objects to datetime.date objects: 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:

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

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

vaultah
  • 44,105
  • 12
  • 114
  • 143
BurningKarl
  • 1,176
  • 9
  • 12