Python返回过去30天的数据
我有这个日志文件样本,我需要计算过去一个月、三个月和一年的条目。这里只是日志文件的几行Python返回过去30天的数据,python,django,python-3.x,python-2.7,datetime,Python,Django,Python 3.x,Python 2.7,Datetime,我有这个日志文件样本,我需要计算过去一个月、三个月和一年的条目。这里只是日志文件的几行 10/14/2015 10:04:25 AM Following file:<open file 'dirs/tmp/bundle_21241.dat.json', mode 'r' at 0x8b73498> has invalid json which is ignored 11/15/2015 10:42:53 PM Following file:<open file 'dirs/tm
10/14/2015 10:04:25 AM Following file:<open file 'dirs/tmp/bundle_21241.dat.json', mode 'r' at 0x8b73498> has invalid json which is ignored
11/15/2015 10:42:53 PM Following file:<open file 'dirs/tmp/bundle_21241.dat.json', mode 'r' at 0xa314498> has invalid json which is ignored
10/21/2015 10:16:42 AM Following hmac:94e301ff67773de56194165451535ba223cd27588221363290fbfcb96d9d0539 with is already in database so dropping
11/21/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 19:11:47+0000+ 12.61 0.430 1686.00
10/21/2015 10:16:42 AM Following hmac:c35330404902c0b1bb5c6d0718407ea12b25a464433bd1e69152ccc0e0b89c9f with is already in database so dropping
10/17/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 19:30:21+0000+ 12.61 0.010 1686.00
10/11/2015 10:16:42 AM Following hmac:8df71a9f6b6f0a0adb48c052767045f37ec34fce9c002a1c0c5ebc38ed500bf8 with is already in database so dropping
10/15/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 19:45:40+0000+ 12.61 0.018 1686.00
12/21/2015 10:16:42 AM Following hmac:fda9f5756461a8bc2922c55e75a31cf4915e6b0d016ecb786666624a0f04a02f with is already in database so dropping
12/10/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 20:01:01+0000+ 12.60 0.048 1686.00
07/21/2015 10:16:42 AM Following hmac:84d9cdb2145b7c3e0fa2d099070b7bd291c652f30ca25c69240e33ebbd2b8677 with is already in database so dropping
10/14/2015 10:04:25 AM以下文件:包含无效的json,将被忽略
2015年11月15日10:42:53下午以下文件:包含无效json,该文件将被忽略
2015年10月21日上午10:16:42以下hmac:94e301ff67773de56194165451535ba223cd27588221363290fbfcb96d9d0539已在数据库中,因此正在删除
2015年11月21日上午10:16:42重复Hmac的数据为:HF 1330010002015-06-15 19:11:47+0000+12.61 0.430 1686.00
2015年10月21日上午10:16:42以下hmac:c35330404902c0b1bb5c6d0718407ea12b25a464433bd1e69152ccc0e0b89c9f已在数据库中,因此正在删除
2015年10月17日上午10:16:42重复Hmac的数据为:HF 1330010002015-06-15 19:30:21+0000+12.61 0.010 1686.00
2015年11月10日上午10:16:42以下hmac:8df71a9f6b6f0a0adb48c052767045f37ec34fce9c002a1c0c5ebc38ed500bf8已在数据库中,因此正在删除
2015年10月15日上午10:16:42重复Hmac的数据为:HF 1330010002015-06-15 19:45:40+0000+12.61 0.018 1686.00
2015年12月21日上午10:16:42以下hmac:FDA9F5756461A8BC292C55E75A31CF4915E6B0D016ECB7866624A0F04A02F已在数据库中,因此正在删除
2015年10月12日上午10:16:42重复Hmac的数据为:HF 1330010002015-06-15 20:01:01+0000+12.60 0.048 1686.00
2015年7月21日上午10:16:42以下hmac:84D9CDB2145B7C3E0FA2D09070B7BD291C652C69240E33EBBD2B8677已在数据库中,因此正在删除
这是我的密码
from datetime import date
from datetime import time
from datetime import datetime
from datetime import timedelta
import os
def fileCount(fileName):
with open(fileName) as FileObj:
Count = 0
today_date = date.today()
One_Year = str(today_date - timedelta(days=365))
One_Month = str(today_date - timedelta(days=30))
Three_Months = str(today_date - timedelta(days=90))
while True:
line = FileObj.readline()
record_date = ('-'.join(line[:10].split('/'))).split(" ")
if not line:
break
if "Following hmac" in line:
try:
convert_date = datetime.strptime(record_date[0], '%m-%d-%Y')
#print "Difference is ", todayDate - convert_date.date()
#print convert_date.date()
date_diff = str(today_date - convert_date.date())
#print dateDiff[:8]
if date_diff[:8] < One_Month:
Count += 1
#print "Last 30 Days Failed HMAC is ", Count
else:
continue
#print convert_date.date()
except ValueError:
print 'This line has a problem:', record_date
print "The Total Number of Failed HMAC is ", Count
# Call The function
def main():
filePath = 'file.txt'
fileCount(filePath)
if __name__ == "__main__":
main()
from datetime导入日期
从日期时间导入时间
从日期时间导入日期时间
从日期时间导入时间增量
导入操作系统
def文件计数(文件名):
打开(文件名)为FileObj时:
计数=0
今天\日期=日期。今天()
一年=str(今天日期-时间差(天数=365))
一个月=str(今天日期-timedelta(天=30))
三个月=str(今天日期-timedelta(天=90))
尽管如此:
line=FileObj.readline()
记录日期=('-'.join(第[:10行).拆分('/')).split(“”)
如果不是直线:
打破
如果行中有“遵循hmac”:
尝试:
convert_date=datetime.strtime(记录日期[0],“%m-%d-%Y”)
#打印“差异是”,todayDate-convert_date.date()
#打印转换日期。日期()
date\u diff=str(今天\u日期-转换\u日期.date())
#打印日期差异[:8]
如果日期差[:8]<一个月:
计数+=1
#打印“过去30天失败的HMAC is”,计数
其他:
持续
#打印转换日期。日期()
除值错误外:
打印“此行有问题:”,记录日期
打印“失败的HMAC总数为”,计数
#调用函数
def main():
filePath='file.txt'
文件计数(文件路径)
如果名称=“\uuuuu main\uuuuuuuu”:
main()
我是个编程新手,不太懂日期算术。目前我得到了返回的答案,但它们似乎没有返回正确的值。目标是循环遍历每一行,并计算最近30、60和365天间隔的行数。我的代码目前包含过去30天的测试,但返回的值错误。您需要将所有内容转换为datetime对象,以便能够比较项目。此外,通过在列表中定义所有不同的范围,并使用Python
计数器()
对它们进行相应的计数,处理所有不同的范围也会更容易。这将更容易扩大范围
from datetime import datetime, timedelta
from collections import Counter
def fileCount(fileName):
log_entry_counts = Counter()
today = datetime.today()
date_ranges = [
('three months', today - timedelta(days=90)),
('month', today - timedelta(days=30)),
('year', today - timedelta(days=365))]
with open(fileName) as f_input:
for line in f_input:
if "Following hmac" in line:
log_date = datetime.strptime(line[:10], '%m/%d/%Y')
for text, dr in date_ranges:
if log_date >= dr:
log_entry_counts[text] += 1
total = 0
for text, count in log_entry_counts.items():
print "Failed HMAC in the last {}: {}".format(text, count)
total += count
print "Total failed HMAC:", total
fileCount('input.txt')
这将使您的输出看起来像:
Failed HMAC in the last three months: 1
Failed HMAC in the last month: 1
Failed HMAC in the last year: 2
Total failed HMAC: 4
您的变量命名错误
one month
应该命名为one\u month
,否则它看起来像一个类名。谢谢。我已经按照要求做了必要的修改。如果您有时间,请阅读感谢材料。你知道如何解决我上面的问题吗?我做了一些更改,但没有返回正确的计数。这里有一个提示:您没有将日期对象与日期对象进行比较,而是将字符串与字符串进行比较<代码>日期差异[:8]<一个月=>您希望如何找到另一个月之后的日期?它们是弦,非常感谢。我正在努力理解这些概念,我知道随着时间的推移,它们会变得更容易