[Tutor] Help with date range

Joel Goldstick joel.goldstick at gmail.com
Tue Feb 9 13:58:03 EST 2016


On Tue, Feb 9, 2016 at 10:34 AM, Chelsea G <cegarcia0323 at gmail.com> wrote:

> So what I am trying to do is take in a csv file and the format of the csv
> file is:
> something, something1, something2,something3, something4, something5,
> something6, something7.
> 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. I
> 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. The piece of code is under the def populate_dict function
> the date_choice part. Here is the code:
>
> import csvimport jsonimport sysimport osfrom collections import
> defaultdictfrom collections import Counter
>
>
> The above imports are a mess! Can you format them correctly?


> UPPER_LIMIT = 5
> LOWER_LIMIT = 4
> class dictionary():
>     def __init__(self):
>         self.dict = defaultdict(list)
>         self.counted_dict = defaultdict(list)
>         self.grouped_dict = defaultdict(list)
>         self.total_dict = defaultdict(list)
>
>
>     def populate_dict(self, filename, date_choice, key_choice):
>         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])
>                 elif date_choice == "none":
>                         self.dict[row[2]].append(row[3])
>                 if key_choice == row[3]:
>                         self.dict[row[2]].append(row[3])
>                 elif key_choice == "none":
>                         self.dict[row[2]].append(row[3])   def
> all_counts(self):
>         data_count = Counter()
>         for key in self.dict.keys():
>             self.counted_dict.update({key: Counter(self.dict[key])})
> # returns the total counts for each application
>     def total_counts(self):
>         self.total_dict.update({'Application': 'Incident Count'})
>         for key in self.dict.keys():
>             total = 0
>             b = Counter(self.dict[key])
>             for value in b:
>                 total += b[value]
>                 self.total_dict.update({key: total})
> # returns the counts of incidents if they are greater than or equal to
> 5, and groups the rest in an "other" category
>     def grouped_counts(self):
>         for key in self.dict.keys():
>             total = 0
>             c = Counter(self.dict[key])
>             self.grouped_dict[key].append(['Description', 'Incident
> Count'])
>             for value in c:
>                 if c[value] >= UPPER_LIMIT:
>                     grouped_list = value, c[value]
>                     self.grouped_dict[key].append(grouped_list)
>                 elif c[value] <= LOWER_LIMIT:
>                     total += c[value]
>             other_list = "other ", total
>             self.grouped_dict[key].append(other_list)
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


Its hard to tell what you want to do.  You should show a small (10 lines?)
input table, and show the results you want.

This is probably much easier to solve by importing csv to a sqlite
database, then running some simple date range queries.  Do you know any sql?

-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays


More information about the Tutor mailing list