i have to make 3 seperate functions that:
function1. Counts the lines in a my 'logfile' and returns the answer in idle.
function2. counts how many times a certain user is in the 'logfile' for instance how many times 'johnny.killed.peter' has been logged in the 'logfile' and returns only the answer
function3. shows what files the user 'johnny.killed.peter' has looked at and writes the names of the files in a new file called 'pole3.out' with one name per row and it has to b ein alphabetical order with the most popular one first.
number 3 can be a dictionary.
thanks for all ur help guys u are really saving my life!
12 1957
i have to make 3 seperate functions that:<snip>
function2. counts how many times a certain user is in the 'logfile' for instance how many times 'johnny.killed.peter' has been logged in the 'logfile' and returns only the answer
function3. shows what files the user 'johnny.killed.peter' has looked at and writes the names of the files in a new file called 'pole3.out' with one name per row and it has to b ein alphabetical order with the most popular one first.
number 3 can be a dictionary.
We'll actually need a sample of your log file for this.
i have to make 3 seperate functions that:
function1. Counts the lines in a my 'logfile' and returns the answer in idle.
<snip>
thanks for all ur help guys u are really saving my life!
-
>>> def NumLines(filename):
-
... try:
-
... f = open(filename)
-
... nLines = len(f.readlines())
-
... print 'There are %d lines in %s' %(nLines, filename)
-
... except IOError, error:
-
... print error
-
...
-
>>> NumLines('module1.py')
-
There are 165 lines in module1.py
-
>>> NumLines('module1.p')
-
[Errno 2] No such file or directory: 'module1.p'
-
>>>
has anyone come up with anything on function 2 and 3? ive been fooling around with this for s couple days and cant seem to get it right. thanks for all the help. -
>>> def NumLines(filename):
-
... try:
-
... f = open(filename)
-
... nLines = len(f.readlines())
-
... print 'There are %d lines in %s' %(nLines, filename)
-
... except IOError, error:
-
... print error
-
...
-
>>> NumLines('module1.py')
-
There are 165 lines in module1.py
-
>>> NumLines('module1.p')
-
[Errno 2] No such file or directory: 'module1.p'
-
>>>
def countclient(filename, client):
f = open(filename)
x = len(f.readlines(client))
return x
does that look like something that could work?
has anyone come up with anything on function 2 and 3? ive been fooling around with this for s couple days and cant seem to get it right. thanks for all the help.
bvdet 2,851
Expert Mod 2GB
has anyone come up with anything on function 2 and 3? ive been fooling around with this for s couple days and cant seem to get it right. thanks for all the help.
If we had a sample of your log file, I am sure we could show you how to get the data from it
does that look like something that could work?
readlines() takes no arguments and it's good practice to close() th file when you are done with it. - def countclient(filename, client):
-
f = open(filename)
-
x = len(f.readlines())
-
f.close()
-
return x
Your posting is a bit out of hand. At 14 posts you are requited to follow all site rules (Posting Guidelines). Most perturbing is your lack of CODE tags. Instructions are right there on the right hand side of the page while you post.
bvdet 2,851
Expert Mod 2GB
def countclient(filename, client):
f = open(filename)
x = len(f.readlines(client))
return x
does that look like something that could work?
Function 2 could be something like this: - user = 'bill.smith'
-
logfile = 'log.txt'
-
-
f = open(logfile)
-
count = 0
-
for line in f:
-
if user in line:
-
count += 1
-
f.close()
-
-
print count
bvdet 2,851
Expert Mod 2GB
As a function, it may look something like this: - def log_count(fn, user):
-
f = open(logfile)
-
logList = [line for line in f if user in line]
-
f.close()
-
-
return logList
-
-
user = 'bill.smith'
-
logfile = 'log.txt'
-
-
logList = log_count(logfile, user)
-
print len(logList)
-
-
>>> def NumLines(filename):
-
... try:
-
... f = open(filename)
-
... nLines = len(f.readlines())
-
... print 'There are %d lines in %s' %(nLines, filename)
-
... except IOError, error:
-
... print error
-
...
-
>>> NumLines('module1.py')
-
There are 165 lines in module1.py
-
>>> NumLines('module1.p')
-
[Errno 2] No such file or directory: 'module1.p'
-
>>>
just a minor note using readlines method. len(readlines()) will count the extra blank line as a line,
eg
sample input:
line1
line2
line3
line4
line5
the above will produce 6. If that's what OP wants then its alright. this can be worked around by stripping off using .strip() :)
another method (out of the many) to count without including blank/new lines -
for num,item in enumerate(open("file")): pass
-
print num
-
function2. counts how many times a certain user is in the 'logfile' for instance how many times 'johnny.killed.peter' has been logged in the 'logfile' and returns only the answer
-
>>> data=open("file").read()
-
>>> data.count("johnny.killed.peter")
-
you make your own function.
bvdet 2,851
Expert Mod 2GB
def countclient(filename, client):
f = open(filename)
x = len(f.readlines(client))
return x
does that look like something that could work?
Now that you have some more information us, can you show us some more of your work? It would be best for you to attempt this on your own, then we can help from there.
bvdet 2,851
Expert Mod 2GB
Assume the visited page is in the format '....GET page_name HTTP....' in each line of the log file. - import re
-
-
# Compile a list of entries for a specific 'user' in log file 'fn'
-
def log_count(fn, user):
-
f = open(logfile)
-
logList = [line for line in f if line.startswith(user)]
-
f.close()
-
return logList
-
-
# Create a dictionary using logList
-
# Dictionary keys are the web pages visited
-
# Dictionary values are the number of visits
-
def visit_count(logList):
-
patt = re.compile(r'GET (.+) HTTP')
-
dd = {}
-
for log in logList:
-
page = patt.search(log).group(1)
-
if page in dd:
-
dd[page] += 1
-
else:
-
dd[page] = 1
-
return dd
-
-
# Create list of value, key pairs from dictionary
-
# Sort on value (decending) and key (ascending)
-
# Disply sorted data
-
def visit_process(dd):
-
def comp(a, b):
-
x = cmp(a[0], b[0])
-
if not x:
-
return cmp(a[1], b[1])
-
return -x
-
pageList = zip(visitDict.values(), visitDict.keys())
-
pageList.sort(comp)
-
for item in pageList:
-
print 'Page "%s" was visited %d time%s.' % (item[1], item[0], ['', 's'][item[0]>1 or 0])
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Willem Ligtenberg |
last post by:
I decided to use SAX to parse my xml file.
But the parser crashes on:
File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in fatalError
raise exception...
|
by: Cigdem |
last post by:
Hello,
I am trying to parse the XML files that the user selects(XML files are
on anoher OS400 system called "wkdis3"). But i am permenantly getting
that error:
Directory0: \\wkdis3\ROOT\home...
|
by: Pir8 |
last post by:
I have a complex xml file, which contains stories within a magazine. The
structure of the xml file is as follows:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<magazine>
<story>...
|
by: Christoph Bisping |
last post by:
Hello!
Maybe someone is able to give me a little hint on this:
I've written a vb.net app which is mainly an interpreter for specialized
CAD/CAM files.
These files mainly contain simple movement...
|
by: Rick Walsh |
last post by:
I have an HTML table in the following format:
<table>
<tr><td>Header 1</td><td>Header 2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>3</td><td>4</td></tr>
<tr><td>5</td><td>6</td></tr>...
|
by: toton |
last post by:
Hi,
I have some ascii files, which are having some formatted text. I want
to read some section only from the total file.
For that what I am doing is indexing the sections (denoted by .START
in...
|
by: Paulers |
last post by:
Hello,
I have a log file that contains many multi-line messages. What is the
best approach to take for extracting data out of each message and
populating object properties to be stored in an...
|
by: Chris Carlen |
last post by:
Hi:
Having completed enough serial driver code for a TMS320F2812
microcontroller to talk to a terminal, I am now trying different
approaches to command interpretation.
I have a very simple...
|
by: charliefortune |
last post by:
I am fetching some product feeds with PHP like this
$merch = substr($key,1);
$feed = file_get_contents($_POST);
$fp = fopen("./feeds/feed".$merch.".txt","w+");
fwrite ($fp,$feed);
fclose...
|
by: Felipe De Bene |
last post by:
I'm having problems parsing an HTML file with the following syntax :
<TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'>
<TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH>
<TH...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM).
In this month's session, the creator of the excellent VBE...
|
by: MeoLessi9 |
last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: marcoviolo |
last post by:
Dear all,
I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
| | |