AtmoSci AtmoSci - 7 days ago 10
Python Question

Trying to count the number of occurrances of each unique hour

I'm trying to read in a file with two separate time formats and then count the number of times each hour of the second time format appears. This is my first Python script and am a bit lost after I thought that I was making significant headway. I'm getting the unique hours in my output file, but no counts and I can't figure out where I'm going wrong.

I'd really appreciate any help that you could provide. Thanks!

This is an example of my file-

KABH, 11:17:00, 04:30:00
KABH, 11:18:00, 04:31:00
KABH, 11:19:00, 04:33:00
KABH, 11:20:00, 05:34:00
KABH, 11:32:00, 05:46:00
KABH, 11:33:00, 02:47:00
KABH, 11:34:00, 02:48:00
KABH, 11:35:00, 02:49:00


This is the code that I'm currently running to get the results-

Python libs
import sys, glob, os, subprocess, calendar, string

# Input file
infile = "test.txt"

# Open file
fin = open(infile,"r")
data = fin.readlines()

# Lists to hold counts
stn = []
cnts = []
UTC = []
NST = []
HRS = []


# Loop over each line and count the number of times each hour is found
for lines in data:

d = string.split(lines,", " )
if not d[0] in stn:
stn.append(d[0])
UTC.append(d[1])
NST.append(d[2])

t = d[2].split(":")
if not t[0] in HRS:
HRS.append(t[0])

# Loop over all the unique times and count how the number of occurrences
for h in HRS:
cnt = 0
for l in data:
t2 = string.split(l,":")
if t2[0] == h:
cnt = cnt + 1
cnts.append(cnt)

# Open a file and write the info
fout = open("data.csv","w")
cnt = 0
while cnt < len(HRS):
fout.write('%02d,%02d\n' % (int(HRS[cnt]),int(cnts[cnt])))
cnt = cnt + 1
fout.close()


Example of current output file-

04,00
05,00
02,00

Answer

You can use dictionary to save hour as key, create an empty list the first time a key is encountered and append 1 into the list. Finally, check the length of list for each key

counter_dict = dict()
with open("sample.csv") as inputs:
    for line in inputs:
        column1, time1, time2 = line.split(",")
        counter_dict.setdefault(time2.split(":")[0].strip(), list()).append(1)

for key, value in counter_dict.iteritems():
    print "{0},{1}".format(key, len(value))

The output is:

02,3
04,3
05,2