Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Python dictionary value assignments.


  • Please log in to reply
1 reply to this topic

#1 Ray Parrish

Ray Parrish

  • Members
  • 91 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Cottage Grove, Oregon
  • Local time:07:24 AM

Posted 08 April 2010 - 09:28 AM

Hello,

I am using a compound dictionary to collect together lines from a server access log into collections of log lines that are sorted by date, and ip address.

Here is what I have been trying to sort the lines into the dictionary.

dates = {date:{thisIPAddress:lineList}}
		  dates[date][thisIPAddress].append(eachLine)

This first method fails, in that the lineList for each ip address index becomes identical due to the appending of eachline's value to the list each time through the loop.

If I leave out the first line in the above code, the second line fails, due to the fact that the index value for thisIPAddress will not exist before it is being called upon to write the list's appendment to.

How can I insure that the dictionary index referred to by thisIPAddress has been created, before using the second line to append an individual value of eachLine to that ip address's line list only?

I have tried the following -

dates = {date:{thisIPAddress:[]}}

But this fails because the list gets reset to empty each time through the loop, and I wind up with empty, or one item lists for lineList in each ip address index's list.

How do I construct a call which only defines a new thisIPAddress index value without giving that index a lineList value to begin with?

Here is some fiddling around in the interpereter I have been trying to no avail -

>>> dates[date][thisIPAddress].append("test")
>>> dates
{'2009 02 15': {'75.272.23.12': ['test']}}
>>> dates[date][thisIPAddress].append("twice entered")
>>> dates
{'2009 02 15': {'75.272.23.12': ['test', 'twice entered']}}
>>> thisIPAddress = "74.123.23.34"
>>> dates = {date:{thisIPAddress:lineList}}
>>> dates
{'2009 02 15': {'74.123.23.34': ['test', 'twice entered']}}
>>> dates = {date:{thisIPAddress:lineList}}
>>> dates
{'2009 02 15': {'74.123.23.34': ['test', 'twice entered']}}
>>> dates[date] = {"75.272.23.12":[]}
>>> dates
{'2009 02 15': {'75.272.23.12': []}}
>>> dates[date] = {"75.272.24.12":[]}
>>> dates
{'2009 02 15': {'75.272.24.12': []}}
>>> dates[date]["75.272.23.12"].append("test line")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: '75.272.23.12'
>>> dates[date]["75.272.24.12"].append("test line")
>>> dates[date] = {"75.272.23.12":[]}
>>> dates
{'2009 02 15': {'75.272.23.12': []}}
>>> dates[date]["75.272.23.12"].append("test line")
>>> dates
{'2009 02 15': {'75.272.23.12': ['test line']}}
>>>

How do I add an ip address as an index within the date index of the outer dictionary? Each time I try, I get a new dictionary with the previous ip address index removed, and the new one inserted. I want the ip address indexes to be accumulative, and am using a dictionary, as I know that subsequent assignments to the same index value will not repeat them.

EDIT: Ok, I can now assign the values the right way with the following code in the loop -

dates[date] = {}
		  dates[date][thisIPAddress]= []
		  dates[date][thisIPAddress].append(eachLine)

Thi works the way I want it to, but now I am having a problem with not being able to skip a line of input in favor of lines that actually contain data. go figure... oh well, that's the subject of another thread, so I'm off to post it.

Thanks for any help you can be.

Edited by Ray Parrish, 08 April 2010 - 09:59 AM.


BC AdBot (Login to Remove)

 


#2 Ray Parrish

Ray Parrish
  • Topic Starter

  • Members
  • 91 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Cottage Grove, Oregon
  • Local time:07:24 AM

Posted 08 April 2010 - 10:35 AM

Hello,

My previously touted code above also did not work, as it resets the value of the dictionary indexes, and list to blank each time through the loop. Here is what I am now trying, which does not work either.

if not dates[date]:
			   dates[date] = {}
		  if not dates[date][thisIPAddress]:
			   dates[date][thisIPAddress]= []
		  dates[date][thisIPAddress].append(eachLine)

This fails with the following error -

Traceback (most recent call last):
  File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2640, in <module>
	VisitorCounter, TotalsCounter = ReadInDaysLog(tempLogName, countryCodes)
  File "/home/ray/LogAnalyzer/iis-log-analyze.py", line 2299, in ReadInDaysLog
	if not dates[date][thisIPAddress]:
KeyError: '61.135.168.82'

since the key value is not defined before being referenced.

How do I test to see if a key value within the dictionary already exists or not? What I need to do is create the key values as they appear, but not write over a previously instantiated key value with an empty list value if the list for that ip address key value has already been assigned to.

EDIT: A kludge method occurs to me wherein I could save an oldDate, and oldIPAddress variable pair which I could check through the loops to see if they vary from thisIPAddress, and date respectively, and only assign a new empty dictionary entry depending on which one of them changes.

However, in the case of reading the ip addresses out of order, with one or more repeating in the log file, you can have the ip address change, but still have previously assigned a dictionary entry for that ip address, or date, so it would be better to be able to detect if a dictionary index already exists or not for each date, or ip address as they come up in the log file.

Thanks for any help you can be.

Edited by Ray Parrish, 08 April 2010 - 03:09 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users