I'm pretty new to python, and I'm working on a script that is reading in a log file and recording different registration events for each MAC address and the number of times they occur.
I am trying to structure the record to look like this as the MAC addresses are unique:
{'mac1' : { 'event1' : count1 , 'event2' : count2, ... } }
The number of unique MAC addresses is variable, as well as the types of events that can happen.
The best I've been able to do so far is this:
{('mac1', 'event1') : count1, ('mac1', event2') : count2, ('mac2', 'event1') : count 1, ...}
This makes it more difficult to print the output as I'd like to do this
mac1: event1=count1 event2=count2 event3=count3
mac2: event 2=count2 event3=count3
mac3: event1=count1 event3= count3
...
The code I have written so far (inside a loop which parses each logfile line): -
reg_MAC_ADDRESS = {}
-
-
#inside loop
-
if reg_MAC_ADDRESS.has_key((mac,event)):
-
reg_MAC_ADDRESS[mac,event] += 1
-
else:
-
reg_MAC_ADDRESS[mac,event] = 1
-
I guess the next step when I sort out the record is how to get the data out of the record and print it to the screen or an output file
Thanks in advance...
7 11130
I'm pretty new to python, and I'm working on a script that is reading in a log file and recording different registration events for each MAC address and the number of times they occur.
I am trying to structure the record to look like this as the MAC addresses are unique:
{'mac1' : { 'event1' : count1 , 'event2' : count2, ... } }
The number of unique MAC addresses is variable, as well as the types of events that can happen.
The best I've been able to do so far is this:
{('mac1', 'event1') : count1, ('mac1', event2') : count2, ('mac2', 'event1') : count 1, ...}
This makes it more difficult to print the output as I'd like to do this
mac1: event1=count1 event2=count2 event3=count3
mac2: event 2=count2 event3=count3
mac3: event1=count1 event3= count3
...
The code I have written so far (inside a loop which parses each logfile line): -
reg_MAC_ADDRESS = {}
-
-
#inside loop
-
if reg_MAC_ADDRESS.has_key((mac,event)):
-
reg_MAC_ADDRESS[mac,event] += 1
-
else:
-
reg_MAC_ADDRESS[mac,event] = 1
-
I guess the next step when I sort out the record is how to get the data out of the record and print it to the screen or an output file
Thanks in advance...
It is possible to have a dictionary with dictionaries in it. -
if reg_MAC_ADDRESS.has_key(mac):
-
reg_MAC_ADDRESS[mac][event] += 1
-
else:
-
reg_MAC_ADDRESS[mac] = dict(event = 1)
-
I'm pretty sure that will work.
Does it work for you?
bvdet 2,851
Expert Mod 2GB
I'm pretty new to python, and I'm working on a script that is reading in a log file and recording different registration events for each MAC address and the number of times they occur.
I am trying to structure the record to look like this as the MAC addresses are unique:
{'mac1' : { 'event1' : count1 , 'event2' : count2, ... } }
The number of unique MAC addresses is variable, as well as the types of events that can happen.
The best I've been able to do so far is this:
{('mac1', 'event1') : count1, ('mac1', event2') : count2, ('mac2', 'event1') : count 1, ...}
This makes it more difficult to print the output as I'd like to do this
mac1: event1=count1 event2=count2 event3=count3
mac2: event 2=count2 event3=count3
mac3: event1=count1 event3= count3
...
The code I have written so far (inside a loop which parses each logfile line): -
reg_MAC_ADDRESS = {}
-
-
#inside loop
-
if reg_MAC_ADDRESS.has_key((mac,event)):
-
reg_MAC_ADDRESS[mac,event] += 1
-
else:
-
reg_MAC_ADDRESS[mac,event] = 1
-
I guess the next step when I sort out the record is how to get the data out of the record and print it to the screen or an output file
Thanks in advance...
Here I am using 'dd' to represent the dictionary: -
if dd.has_key(mac):
-
if dd[mac].has_key(event):
-
dd[mac][event] += 1
-
else:
-
dd[mac].update({event: 1})
-
else:
-
dd[mac] = {event: 1}
Example output: >>> dd
{'00:23:B6:58:74:7B': {'FFF': 4, 'GGG': 2, 'DDD': 6}, '00:16:C6:99:23:3D': {'FFF': 6, 'GGG': 3, 'DDD': 9}, '00:22:A6:58:79:7C': {'FFF': 2, 'GGG': 1, 'DDD': 3}}
>>> bvdet 2,851
Expert Mod 2GB
To print the dictionary: - for mkey in reg_MAC_ADDRESS:
-
for skey in reg_MAC_ADDRESS[mkey]:
-
print 'MAC: %s Event: %s Event Count: %d' % (mkey, skey, reg_MAC_ADDRESS[mkey][skey])
Output:
>>> MAC: 00:23:B6:58:74:7B Event: FFF Event Count: 4
MAC: 00:23:B6:58:74:7B Event: GGG Event Count: 2
MAC: 00:23:B6:58:74:7B Event: DDD Event Count: 6
MAC: 00:16:C6:99:23:3D Event: FFF Event Count: 6
MAC: 00:16:C6:99:23:3D Event: GGG Event Count: 3
MAC: 00:16:C6:99:23:3D Event: DDD Event Count: 9
MAC: 00:22:A6:58:79:7C Event: FFF Event Count: 2
MAC: 00:22:A6:58:79:7C Event: GGG Event Count: 1
MAC: 00:22:A6:58:79:7C Event: DDD Event Count: 3
Here I am using 'dd' to represent the dictionary: -
if dd.has_key(mac):
-
if dd[mac].has_key(event):
-
dd[mac][event] += 1
-
else:
-
dd[mac].update({event: 1})
-
else:
-
dd[mac] = {event: 1}
Example output:>>> dd
{'00:23:B6:58:74:7B': {'FFF': 4, 'GGG': 2, 'DDD': 6}, '00:16:C6:99:23:3D': {'FFF': 6, 'GGG': 3, 'DDD': 9}, '00:22:A6:58:79:7C': {'FFF': 2, 'GGG': 1, 'DDD': 3}}
>>>
That's exactly what my original code looked like but, I didn't think there's a need for the nested if because everytime we create a new mac we give it an event. So every mac will have an event, so there's no need to check if it does. Am I right?
Replying to my own post, but I figured it out. -
reg_MAC_ADDRESS = {}
-
-
# inside loop
-
# build up the dictionary/database with the registration events and counts from log file
-
if reg_MAC_ADDRESS.has_key(mobile_mac_address):
-
if reg_MAC_ADDRESS[mobile_mac_address].has_key(mobile_cause):
-
reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] += 1
-
else:
-
reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] = 1
-
else:
-
reg_MAC_ADDRESS[mobile_mac_address] = {mobile_cause : 1}
-
This is probably not the most elegant way of doing things, but it seems to work...
That's exactly what my original code looked like but, I didn't think there's a need for the nested if because everytime we create a new mac we give it an event. So every mac will have an event, so there's no need to check if it does. Am I right?
Oh wow, I'm wrong. That was stupid, just because a mac has an event doesn't mean that it will have the one we need. So, I'm sorry.
bvdet 2,851
Expert Mod 2GB
Replying to my own post, but I figured it out. -
reg_MAC_ADDRESS = {}
-
-
# inside loop
-
# build up the dictionary/database with the registration events and counts from log file
-
if reg_MAC_ADDRESS.has_key(mobile_mac_address):
-
if reg_MAC_ADDRESS[mobile_mac_address].has_key(mobile_cause):
-
reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] += 1
-
else:
-
reg_MAC_ADDRESS[mobile_mac_address][mobile_cause] = 1
-
else:
-
reg_MAC_ADDRESS[mobile_mac_address] = {mobile_cause : 1}
-
This is probably not the most elegant way of doing things, but it seems to work...
It looks fine to me. It's almost identical to mine! :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Garry Hodgson |
last post by:
a colleague of mine has seen an odd problem in some code of ours.
we initially noticed it on webware, but in distilling a test case it seems
to be strictly a python issue. in the real system, it...
|
by: ajikoe |
last post by:
Hello,
Is anyone has experiance in running python code to run multi thread
parallel in multi processor. Is it possible ?
Can python manage which cpu shoud do every thread?
Sincerely Yours,...
|
by: Shane |
last post by:
Hi,
Thanks in advance for the help. I have been to many websites and tried
several solutions to my problem, but have fixed part of it. It's time to
come humbly to the newsgroups for help :-)
...
|
by: Tree menu using XML |
last post by:
I have one XML file that has nodes and sub node and each and every node has
the attribute call visible if its value is true then diplay this node else
don't display thid node, but this condition i...
|
by: mimmo |
last post by:
Hi! I should convert the accented letters of a string in the correspondent
letters not accented. But when I compile with -Wall it give me:
warning: multi-character character constant
Do the...
|
by: Fuzzy.wonderdog |
last post by:
Hello,
I hope some one can help me, at least give me a little direction.
I have the need to insert many records into the same table at once.
Not very many data points (<5), but many (30+)...
|
by: Olly |
last post by:
Hello Everyone!
Could someone please have a look at my JS Form I posted
below....Something wrong there, but I don't understand what's exactly.
Many thanks.
Olly
...
|
by: Jay |
last post by:
I have a multi threaded VB.NET application (4 threads) that I use to send
text messages to many, many employees via system.timer at a 5 second
interval. Basically, I look in a SQL table (queue) to...
|
by: Wayne |
last post by:
I wish to remove the last newline (if any) from a string.
This is where I started:
var text = "hello\n";
text = text.replace( /^(.*)\n?$/, "$1" );
This seems to work, but not if the test has...
|
by: Guilherme Polo |
last post by:
On Wed, Sep 3, 2008 at 8:57 PM, Kevin McKinley <kem1723@yahoo.comwrote:
Come on.. "help on lines 384-403", that is not a good way to look for
help. You are supposed to post some minimal code that...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |