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?

2
  • 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. Commented Jan 11, 2015 at 10:30
  • @Burhan Khalid You are right my mistake. what I want is the string representation of the date Commented Jan 11, 2015 at 10:31

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

5 Comments

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.
Nah, it appears he has mentioned I was wrong in the comments.
Thanks Kasra and Bair. Not that it is very important but I am not a 'he';)
@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).
Thanks a lot for your [email protected]. Sebastian. I did not fully understand your point about exception here. Can you elaborate more?
-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.