import csv def hit_count(file): try: with open(file, newline='') as fin: reader = csv.reader(fin) custid = set() count = 0 hitcount = 0 #the following loop pulls out unique customer id's and puts them into a set called "custid" for row in reader: if row != "0": continue #print(row) custid.add(row) count +=1 print("There were", count, "events in the collection.\n") print("There were", len(custid), "unique customer's collected\n") stats = list(custid) print(stats) #the following loop counts the number of times each unique custid was collected and appends the count to "stats" with open(file, newline='') as fin: reader = csv.reader(fin) for item in range(len(stats)): item = stats.pop(0) #print("Checking", item) for row in reader: #print("Checking", item, "against", row) if item == row: hitcount += 1 stats.append((item, hitcount)) for item in stats: print(item) except IOError as err: print("File Error", str(err)) hit_count("Austin_12032013.csv")
I created "stats" because I'm planning on further functionality that will add to the list of analysis items. So eventually I will have a list of lists that include customer id, number of times each customer came to the store, which store they visited, etc.
At first the second loop wouldn't work at all. When I added the redundant "with open..." and csv reader lines then the loop would work with the first custid in the set, but stops after that. I can't figure out why the program won't iterated through the entire csv file for each of the customer id's in the custid set.