尝试计算每个独特小时的出现次数
Trying to count the number of occurrances of each unique hour
我正在尝试读取具有两种不同时间格式的文件,然后计算第二种时间格式每小时出现的次数。这是我的第一个 Python 脚本,在我认为我取得了重大进展后有点迷茫。我在我的输出文件中得到了独特的时间,但没有计数,我无法弄清楚我哪里出错了。
如果您能提供任何帮助,我将不胜感激。谢谢!
这是我的文件的例子-
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
这是我目前运行得到结果的代码-
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()
当前输出文件示例-
04,00
05,00
02,00
您可以使用dictionary
将hour
保存为键,第一次遇到键时创建一个空列表并将1 添加到列表中。最后,检查每个键的列表长度
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))
输出为:
02,3
04,3
05,2
我正在尝试读取具有两种不同时间格式的文件,然后计算第二种时间格式每小时出现的次数。这是我的第一个 Python 脚本,在我认为我取得了重大进展后有点迷茫。我在我的输出文件中得到了独特的时间,但没有计数,我无法弄清楚我哪里出错了。
如果您能提供任何帮助,我将不胜感激。谢谢!
这是我的文件的例子-
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
这是我目前运行得到结果的代码-
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()
当前输出文件示例-
04,00
05,00
02,00
您可以使用dictionary
将hour
保存为键,第一次遇到键时创建一个空列表并将1 添加到列表中。最后,检查每个键的列表长度
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))
输出为:
02,3
04,3
05,2