[Tutor] Help with date range

Alan Gauld alan.gauld at btinternet.com
Tue Feb 9 19:38:02 EST 2016

On 09/02/16 15:34, Chelsea G wrote:

> Then in the csv file I want to search for a certain range of dates like
> 1/3/2016 - 2/3/2016. I can get individual dates but not a range of dates. 

Can ytou simplify the problem momentaruily?
Can you create a list of dates then search that list for a
range of dates? That is forget about the csv file issues
and focus on the date problem? That might make it easier
to see where you are having the issues.

> have an if elif statement to read row5 which is the date row. My if
> statement is the initial pass at returning the values within the date range
> and then my elif is the parameter and if there is no parameter passed in
> then it returns all data. I am having some trouble trying to pass in a date
> range parameter. 

How are you trying to represent the date range? As two dates?
Or as date and a timedelta object? Or some thing else?(what?)

>     def populate_dict(self, filename, date_choice, key_choice):

Unfortunately in Python we can't tell much about the types
of your parameters from the declaration.

>         with open(filename, 'rb') as f:
>             reader = csv.reader(f)
>             next(reader, None) #
>             for row in reader:
> 		if date_choice == row[5]:
> 			self.dict[row[2]].append(row[3])

row[5] will be a string so this suggests your date_choice is
a string representation of a single date? That obviously cannot
be a range you need a start and end point. And then you need
to see if the target date lies between those two points. And
for that you need a way to compare them. Comparing dates as
a general problem is very, very difficult.

Fortunately the datetime module and the timedelta object
within it simplifies things for your use case.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list