RichNS
RichNS

Reputation: 19

dynamic variables from list

This is similar to, Python creating dynamic global variable from list, but I'm still confused.

I get lots of flo data in a semi proprietary format. I've already used Python to strip the data to my needs and save the data into a json file called badactor.json and are saved in the following format:

[saddr as a integer, daddr as a integer, port, date as Julian, time as decimal number]

An arbitrary example [1053464536, 1232644361, 2222, 2014260, 15009]

I want to go through my weekly/monthly flo logs and save everything by Julian date. To start I want to go through the logs and create a list that is named according to the Julian date it happened, i.e, 2014260 and then save it to the same name 2014260.json. I have the following, but it is giving me an error:

#!/usr/bin/python

import sys
import json
import time
from datetime import datetime

import calendar
#these are varibles I've had to use throughout, kinda a boiler plate for now
x=0
templist2 = []
templist3 = []
templist4 = []
templist5 = []
bad = {}
#this is my list of "bad actors", list is in the following format
#[saddr as a integer, daddr as a integer, port, date as Julian, time  as decimal number]
#or an arbitrary example [1053464536, 1232644361, 2222, 2014260, 15009]
badactor = 'badactor.json'

with open(badactor, 'r') as f1:
    badact = json.load(f1)
f1.close()

for i in badact:
    print i[3] #troubleshooting to verify my value is being read in
    tmp = str(i[3])
    print tmp#again just troubleshooting
    tl=[i[0],i[4],i[1],i[2]]
    bad[tmp]=bad[tmp]+tl
    print bad[tmp]

Trying to create the variable is giving me the following error:

Traceback (most recent call last):
  File "savetofiles.py", line 39, in <module>
    bad[tmp]=bad[tmp]+tl
KeyError: '2014260'

Upvotes: 0

Views: 164

Answers (2)

martineau
martineau

Reputation: 123463

I suggest you initialize bad to be an empty collections.defaultdict instead of just regular built-in dict. i.e.

import collections
  ...
bad = collections.defaultdict(list)

That way, initial empty list values will be created for you automatically the first time a date key is encountered and the error you're getting from the bad[tmp]=bad[tmp]+tl statement will go away since it will effectively become bad[tmp]=list()+tl — where the list() call just creates and returns an empty list — the first time a particular date is encountered.

It's also not clear whether you really need the tmp = str(i[3]) conversion because values of any non-mutable type are valid dictionary (or defaultdict) keys, not just strings — assuming i[3] isn't a string already. Regardless, subsequent code would be more readable if you named the result something else, like julian_date = i[3] (or julian_date = str(i[3]) if the conversion really is required).

Upvotes: 0

Ivan
Ivan

Reputation: 1507

By the time your code is executed, there is no key "2014260" in the "bad" dict.

Your problem is here:

bad[tmp]=bad[tmp]+tl

You're saying "add t1 to something that doesn't exist."

Instead, you seem to want to do:

bad[tmp]=tl

Upvotes: 2

Related Questions