试图在JSON中的行上使用defaultdict

时间:2015-12-30 19:29:22

标签: python

我正在通过JSON行进行插入,并尝试使用唯一的playerID创建所有事件的字典,但我不断收到错误AttributeError: 'collections.defaultdict' object has no attribute 'append'

我正在使用的代码如下:

import json, csv, sys
from collections import defaultdict, Counter
lastTimeByPlayer = defaultdict(int)
uniquePlayerID = defaultdict(list)
lasttime = 0
totalCircuits = 0
playerID = 0

allCircuitsByUniquePlayerID = defaultdict(lambda : defaultdict(int))

for line in infile:
datum = json.loads(line)
time = int(datum['timestamp'])
try:
    pids = list(datum['playspace_ids'])
except:
    pids = []
for player in pids:
    if (time - lastTimeByPlayer[player]) > int(sys.argv[1]): #sys args is a set interval "dead time" interval that indicates a new player has arrived
        playerID = playerID +1
        uniquePlayerID[player].append(playerID)
    lastTimeByPlayer[player] = time
    if datum['key'] == "MakeCircuitCreated":
        allCircuitsByUniquePlayerID[playerID].append(time)

我觉得这与我如何创建allCircuitsByUniquePlayerID[playerID]密钥的每个实例有关,但我不太确定。提前致谢

1 个答案:

答案 0 :(得分:0)

我只是猜测你的意图,但我想你要么:

allCircuitsByUniquePlayerID = defaultdict(list)

或者你错过了某种电路ID,你想要的是:

        allCircuitsByUniquePlayerID[playerID][circuitID].append(time)