I wrote a python script that takes a csv which has dates in an M/D/YYYY format, then matches them with dates in an xlwt generated .xls file. I was wondering why only some dates are matched and then I noticed that the only dates that were matched were strings that were 10 characters long (11/14/2011) and dates that were not 10 characters long (1/14/2011 or 1/7/2012) were not matched. How do I get the csv file to save the dates in 10 character long strings? Or should I convert the csv file into a txt file?
Share
csvdoesn’t know anything about dates; it just reads text. You are comparing strings with other strings, and'1/7/2012'and'01/07/2012'are different strings altogether.You’d need to parse the dates into something that can be compared, when reading. Use the
datetime.datetime.strptime()function for that:The
strptime()format above would turn both1/7/2012and01/07/2012into adatetime.date(2012, 1, 7), and two copies of thatdate()instance would compare as equal.