AtmoSci AtmoSci - 1 year ago 64
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:

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

# 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

# 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

Example of current output file-


Answer Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download