Chinaunix首页 | 论坛 | 博客
  • 博客访问: 61938
  • 博文数量: 17
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-07 11:14
个人简介

。roth lower(substr(ename,2,length(ena)))

文章分类

全部博文(17)

文章存档

2017年(14)

2013年(3)

我的朋友

分类: Python/Ruby

2017-02-20 23:35:38


取文件中的时间,改为时间戳,与当前时间对比

点击(此处)折叠或打开

  1. import time
  2. import datetime

  3. print datetime.datetime.now()

  4. def compare_time(l_time,start_t,end_t):

  5.     s_time = time.mktime(time.strptime(start_t,'%Y%m%d%H%M')) # get the seconds for specify date
  6.     e_time = time.mktime(time.strptime(end_t,'%Y%m%d%H%M'))
  7.     log_time = time.mktime(time.strptime(l_time,'%Y-%m-%d %H:%M:%S'))

  8.     if (float(log_time) >= float(s_time)) and (float(log_time) <= float(e_time)):

  9.         return True

  10.     return False

  11. def GetLine():
  12.     fo = open("0220.txt", 'r+')
  13.     lines = []
  14.     str = []
  15.     while 1:
  16.         line = fo.readline()
  17.         str = line[0:20]
  18.     
  19.         if not line:
  20.             break
  21.         pass
  22.         lines.append(line)
  23.         str.append( line[0:20])
  24.     print lines
  25.     print str
  26.     return lines, str

二,设置一个时间区间如 500ms,匹配所有符合条件的记录并输出


点击(此处)折叠或打开

  1. from datetime import datetime, timedelta

  2. def saveNum(str1):
  3.     fow = open('rst.log', 'ab+')
  4.     fow.write(str1 + '\n')
  5.     fow.close()


  6. def GetZoneTimeNums(timeStar=None,millTime=None):
  7.     x = 0
  8.     fo = open("0220.txt", 'r+')
  9.     timeStarList = []
  10.     OList = []

  11.     for (num, value) in enumerate(fo):
  12.         line = value
  13.         print num, line
  14.         timeLine = line[0:26]
  15.         timeDate = datetime.strptime(timeLine, '%Y-%m-%d %H:%M:%S.%f')

  16.         if not timeDate <= timeStar:
  17.             str1 = str(timeStar) + '\t' + str(x)
  18.             saveNum(str1)
  19.             print '不符合的行',num, timeStar, timeDate
  20.             print 'No'
  21.             while True:
  22.                 x = 0
  23.                 if timeStar < timeDate:
  24.                     #时间区间移动,直到覆盖现有时间点
  25.                     timeStar =timeStar + timedelta(milliseconds=500)
  26.                 else:
  27.                     break
  28.                 print 'x =1 timeStar add', timeDate, timeStar
  29.             print 'timeStar', timeDate, timeStar
  30.         if timeStar >= timeDate:
  31.             x = x + 1
  32.             #记录匹配过程
  33.             OList.append(str(timeStar)+':'+str(timeDate)+':'+str(x))
  34.             print timeStar, timeDate, x
  35.             print 'YES'
  36.             print timeStar, timeDate
  37.             continue
  38.     str1 = str(timeStar) + '\t' + str(x)
  39.     saveNum(str1)

  40.     fo.close()
  41.     return OList


  42. if __name__ == '__main__':
  43.     #设置起点
  44.     timeS = datetime.strptime('2017-02-20 23:12:22.511111', '%Y-%m-%d %H:%M:%S.%f')
  45.     #传入起点和统计粒度
  46.     lis = GetZoneTimeNums(timeS, 500)
  47.     print lis


















阅读(651) | 评论(0) | 转发(0) |
0

上一篇:一点其他小巧

下一篇:py 之 内置函数模块

给主人留下些什么吧!~~