Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 14th, 2004, 05:17 AM
123456789 123456789 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 4 123456789 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 40 sec
Reputation Power: 0
split lines

I am reading in a log file in the following format:

64.68.82.38 - - [16/Nov/2003:08:17:57 +0000] "GET /pipermail/gm2/2003-September/author.html HTTP/1.0" 200 4537

and i am trying to count the number of accesses to the file pipermail

Code:
def ProcessLine ()
       count = 0
       for line in open(logfile.data).readlines():
             words = string.split(line)
             space = string.split(words[6],' ')
             line = string.split('/')
             site = space[0]
             print 'Filename:', site
             if site == 'pipermail':
                  count += 1
       print 'No. of accesses in pipermail:',count


this code displays all the files but it only gives a count of 0 which is obviously wrong.

Edit: Added [ code ] tags, this way you're code will maintain its indentation; very important in Python

Last edited by netytan : January 14th, 2004 at 07:55 AM.

Reply With Quote
  #2  
Old January 14th, 2004, 08:28 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Ok, the problem here is that site is never 'pipermail' so count never becomes more than 0

What you need to do is check if site starts with '/pipermail/', where i could've fixed your function it just seemed easier to just start over...

Code:
#!/usr/bin/env python

def processline():
	count = 0
	for line in open('test.txt', 'r'):
		words = line.split()
		words = words[6]
		print 'Filename:', words
		if words.startswith('/pipermail/'):
			count = count + 1
	print 'No. of accesses in pipermail:', count

if __name__ == '__main__':
	processline()


another thing i knowticed, you're fuction would also cause a SyntaxError because theres no ':' after you define your function

Edit: fixed the typo in the shebang

Mark.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : January 14th, 2004 at 03:20 PM.

Reply With Quote
  #3  
Old January 14th, 2004, 11:32 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 9th Plane (9000 - 9499 posts)
 
Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,406 Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 46th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 10 h 20 m 32 sec
Reputation Power: 4080
You have a minor typo too netytan. Your first line reads:
#!/usr/bun/env python
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

Reply With Quote
  #4  
Old January 14th, 2004, 03:26 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Thanks Scorpi , the thing about windows, it runs regardless so its hard to debug little things like that ... We'll spotted!

Mark.

Reply With Quote
  #5  
Old January 19th, 2004, 07:29 AM
123456789 123456789 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 4 123456789 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 40 sec
Reputation Power: 0
logfile help

Code:
#!/usr/bin/python

import sys, string, getopt

def count_lines ():
       linecount = 0
       logfile = opt[1]
       for  line in open(logfile).readlines():
              linecount = linecount + 1

        print "accesses:", linecount

def processline ():
       count = 0
       total = 0
       logfile = opt[1]
       for line in open(logfile).readlines():
             words = string.split(line)
             words = words[6]
             total = total + 1
             if words.startswith('/pipermail'):
                 count = count + 1
                 percentage = count * 100 / total
            print  'Accesses directory:',count,'(',percentage,'%)'

try:
      optlist, list = getopt.getopt(sys.argv[1:], ':p:f:')

except getopt.GetoptError:
         Usage()
         print "called exception"
         sys.exit(2)

for opt in optlist
      if opt[0] == '-p':
           processline()
      if opt[0] == '-f':
          count_lines()


when this code is run in a terminal using the following options:

./python.py -f access.data -p access.data
i get the following output:

accesses: 6
Accesses directory: 3 ( 50% )

but instead of putting the filename after the -p option i would like to be able to put in a directory name such as:
/pipermail or,
/Glamorgan
and then the output would display the number of access to that specific director and not the whole file.

The logfile which I am processing contains the following data (just a sample as it contains many entries some with /pipermail and somewith /Glamorgan)

213.1.145.506 --{12/Dec/2002:07:41:19 +0000} " GET /pipermail/notes/web/m2f.html HTTP/1.0" 301 -

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > split lines

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap