[Tutor] Help with date range
Chelsea G
cegarcia0323 at gmail.com
Tue Feb 9 10:34:18 EST 2016
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
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)
More information about the Tutor
mailing list