Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python中的函数比较时间戳(与Delorean)_Python - Fatal编程技术网

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

使用Python中的函数比较时间戳(与Delorean),python,Python,我有一段基本的Python代码,我试图在其中比较一次和另一次(我必须承认我对Python有点陌生)。我使用Delorean将字符串转换为一个历元整数,并将其与变量lastTime进行比较。我其余的代码都可以工作,但只要我尝试添加新函数,我的读数就会变为空白。我正在努力解决这个问题,因为Python没有抛出任何错误,它只是什么都没有给我 以下是完整的代码: import random,sys,csv, delorean from collections import defaultdict fro

我有一段基本的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(行['timestamp',lastTime)
我知道回归现在做的不多,但这不应该影响它的其余部分,对吗


这是我的

示例,多亏了mkrieger1,我能够更好地检查我的错误,结果是我的数据库如何构造日期以及Delorian default如何读取日期的解析问题

以下是已修复的函数:

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'

如果有人需要它,有一个解析日期和解释。在修补过程中,我还需要添加来自delorean import epoch的

如果您需要
时间戳
和调整后的时间戳之间的差异,为什么不使用
excel
(或
googlesheets
)为您进行计算?听起来更容易我猜
tenMinInterval
引发了一些异常,然后执行exept子句中的
print'
,在屏幕上填充空行。用
raise
替换
print'
,查看发生了什么。一般来说,最好列出要捕获的异常类型(例如,IOError除外,
),这样您就不会意外捕获任何您没有预料到的异常。@letsc,因为在某些情况下,我将提取+10000个项目,这样做是禁止的(手动将其向下拉并编辑,然后在python中对其进行操作,增加了一系列在python中应该很容易完成的步骤)而且,一旦我发现了差异,我想用它做更多的事情——这只是让我进入下一个阶段step@mkrieger1-谢谢,我使用了
raise
来获取所需的错误!由于数据库格式化时间戳的方式,我需要进行另一次解析。现在似乎正在工作(或者至少我正在处理下一个问题)