python - 使用 Python 中的函数比较时间戳(使用 Delorean)

标签 python

我有一段基本的Python代码,我试图在其中一次与另一次进行比较(我必须承认我对Python有点陌生)。我使用 Delorean 将字符串转换为纪元整数,并将其与变量 lastTime 进行比较。我的其余代码可以工作,但是一旦我尝试添加新函数,我的读数就会变成空白。我正在努力解决这个问题,因为 Python 没有抛出任何错误,它只是没有给我任何东西。

这是完整的代码:

import random,sys,csv, delorean
from collections import defaultdict
from delorean import Delorean
from delorean import parse

size = ['small','medium','large']
color = ['blue','red','green']
body = ['fish','squid']

fishparts = defaultdict(set)
lastfish = defaultdict(str)
lastTime = 0


def tenMinInterval(ts, lt): # HAVING PROBLEMS HERE
    global lastTime
    curTime = Delorean(ts).epoch()
    if curTime > lt+600000: # timestamp is a string and lastTime is an int
        # return True
        lastTime = curTime
        return 'Upadted Time'
    else:
        # return False
        return 'Not Upadted'

def complexityFish(ps,pf):
    score = 1
    if ps == 'medium':
        score += 1
    elif ps == 'large':
        score += 2
    if pf == 'squid':
        score += 2
    return str(score)

def diffPrev(a,la,b,lb,c,lc):
    score = 0
    if a != la:
        score += 1
    if b != lb:
        score += 1
    if c != lc:
        score += 1
    return str(score)

def diffUniq(player,x):
    score = 0
    for e in x:
        if e not in fishparts[player]:
            score += 1
        fishparts[player].add(e)
    return str(score)


def parseOneFish(p_player,p_fish):
    player  = p_player
    fish    = p_fish
    if lastfish[player] != '':
        ls,lc,lt = lastfish[player].split(' ')
    else:
        ls = lc = lt = ''
    s,c,t = fish.split(' ')
    lastfish[player] = fish
    return((complexityFish(s,t),diffPrev(s,ls,c,lc,t,lt),diffUniq(player,[s,c,t])))


csvfilename = sys.argv[1]
csvdata = csv.DictReader(open(csvfilename,'rb'),delimiter=',')
x = False
for line in csvdata:
    if not x:
        print ','.join([k for k in line]),
        print ',complexity,diffprev,diffuniq'
        x = True
    try:
        cx,dp,du = parseOneFish(line['playerID'],line['fishType'])
        tm = tenMinInterval(line['timestamp'], lastTime) # HAVING PROBLEMS HERE
        print ','.join([line[k] for k in line]) + ',',
        print ','.join([cx,dp,du])
        print ','.join(tm)
    except:
        print ''  

我正在努力解决的部分位于函数 tenMinInterval 中,并位于 tm = tenMinInterval(line['timestamp'], lastTime) 的底部 我知道返回现在没有多大作用,但这不应该影响其余部分,对吗?

这是我的 csv file as well 的示例

最佳答案

多亏了 mkrieger1,我能够更好地检查我的错误,最终导致我的数据库如何构造日期以及 Delorian 默认值如何读取它成为一个解析问题。

这是修复的功能:

def tenMinInterval(ts, lt):
    global lastTime
    strTime = delorean.interface.parse(ts, dayfirst=False, yearfirst=False)
    curTime = strTime.epoch()
    print curTime
    if curTime > lt+600: # timestamp is a string and lastTime is an int
        # return True
        lastTime = curTime
        print 'Time Updated'
    else:
        # return False
        print 'Time not Updated'

如果有人需要它,这里有解析日期的解释 herehere 。在修补时,我还需要添加 from delorean import epoch

关于python - 使用 Python 中的函数比较时间戳(使用 Delorean),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28932531/

相关文章:

python - 在 Python 中快速拼接文件并获取它们的 md5

python - 以最快的方式查找重复的图像

python - 基于列条件的 Pandas Dataframe 过滤

Python-生成特定自相关的数组

python - 从 Google 云端硬盘获取 Whatsapp 备份文件

Python/OpenCV - 基于机器学习的 OCR(图像到文本)

python - 如何从 csv 文件获取存储聚合值的字典字典

python - 为什么python的re.search方法挂了?

Python 电子邮件 - 8 位 MIME 支持

Python scipy.numpy.convolve 和 scipy.signal.fftconvolve 不同的结果