Daniel Vazio
Daniel Vazio

Reputation: 13

how to get date from datetime format-python

how can i extract the date only without the time in same row of my csv files?

import csv
from datetime import datetime
with open('mar.csv','rb') as csvfile:

    reader=csv.reader(csvfile,delimiter=',')
    reader.next()
    d = datetime(row[35] for row in reader)
    print(d.strftime("%Y %m %d"))

Example of the column:

8/9/2014 14:44
8/9/2014 14:44
8/12/2014 14:54
8/12/2014 10:56
8/9/2014 20:03
8/12/2014 16:09
8/12/2014 10:13
8/12/2014 23:31
8/13/2014 21:31
8/12/2014 15:44
8/12/2014 15:47
8/11/2014 9:45



TypeError: an integer is required

Upvotes: 0

Views: 9021

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 881695

Your code has other problems beyond the one in the Q's subject, but anyway here's one clean approach to do what you ask + something of what you don't ask but need...:

for row in reader:
    dt = datetime.strptime(row[35], '%m/%d/%Y %H:%M')
    d = date(year=dt.year, month=dt.month, day=dt.day)
    print(d.strftime("%Y %m %d"))

Alternatively,

    d = dt.date()

will work fine (and faster) if you find it readable enough for your purposes.

Upvotes: 4

Related Questions