Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95315

Re: AttributeError

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: AttributeError
Date 2015-08-12 21:16 +0000
Organization A noiseless patient Spider
Message-ID <mqgd3p$8np$5@dont-email.me> (permalink)
References (7 earlier) <55CB7823.8010402@mrabarnett.plus.com> <CALa1VB52wjJesc8Skp=Uiu9fY-NMvfd8cPVEpnkxdX-22+xO6w@mail.gmail.com> <55CB815B.8070800@mrabarnett.plus.com> <mailman.131.1439401060.3627.python-list@python.org> <5a09701f-06d3-42dd-aa96-59920de76044@googlegroups.com>

Show all headers | View raw


On Wed, 12 Aug 2015 11:35:03 -0700, Ltc Hotspot wrote:

> How do I define time in the revised code ?

I'm guessing that you have a line of input like:

word word word timestamp word word word word

and that timestamp looks something like:

hh:mm:ss

Start of by defining a list with 24 elements all integer 0.

Open the file.

for each line, first you trim it, then you split it on spaces, you take 
the relevant element of the list of bits as the timestamp, and then you 
split that on the ':' character, then you take the int value of the hours 
element of that list and use that as the index into your list to update 
the count.

Processing a file takes very few lines of actual code. There is no need 
to use a dictionary, you can do it with a single list and no sorting. 
This is a solution in 8 lines, and assumes that the actual requirement is 
to count, for each hour, the number of log messages generated in that 
hour, and then display, listed by hour, the number of messages generated 
in each hour during the day. It also assumes that there is a timestamp of 
the form hh:mm:ss that always appears at the same word position X in each 
line in the file, and that the hours record always at position Y in the 
timestamp.

c = [0 for i in range(24)]
f = open(filename,'r')
for l in f:
    h = int(l.strip().split()[X].split(':')[Y])
    c[h] = c[h] + 1
f.close()
for i in range(24):
    print '{:02d} {}'.format(i, c[i])

-- 
Denis McMahon, denismfmcmahon@gmail.com

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-11 17:01 -0700
  Re: AttributeError leo kirotawa <kirotawa@gmail.com> - 2015-08-11 21:16 -0300
  Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 01:26 +0100
  Re: AttributeError Chris Angelico <rosuav@gmail.com> - 2015-08-12 10:49 +1000
  Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 01:58 +0100
    Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-11 22:03 -0700
      Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 16:12 +0100
      Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 15:50 +0000
        Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 09:29 -0700
          Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 17:45 +0100
          Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 09:57 -0700
          Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 18:24 +0100
          RE: AttributeError "Clayton Kirkwood" <crk@godblessthe.us> - 2015-08-12 10:31 -0700
          Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 10:37 -0700
            Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 11:35 -0700
              Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 19:59 +0100
              Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 12:05 -0700
                Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 20:38 +0000
                RE: AttributeError "Clayton Kirkwood" <crk@godblessthe.us> - 2015-08-12 14:15 -0700
                Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 14:32 -0700
                Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 22:45 +0100
                Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 22:51 +0100
                Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 14:04 -0700
                Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-13 10:37 +0100
                Re: AttributeError Ben Finney <ben+python@benfinney.id.au> - 2015-08-13 21:08 +1000
              Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 21:04 +0100
              Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 21:16 +0000
                Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 23:35 +0100
                Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 16:05 -0700
                Re: AttributeError Emile van Sebille <emile@fenx.com> - 2015-08-12 16:15 -0700
                Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-13 00:16 +0100
                Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 16:46 -0700
                Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-13 09:15 +0000
                Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-13 02:41 -0700
                Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-13 11:06 +0000
                Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-13 00:56 +0100
          Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 20:28 +0000
            Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 21:58 +0100
  Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 03:46 +0100
  Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 04:32 +0100
  Re: AttributeError Chris Angelico <rosuav@gmail.com> - 2015-08-12 13:49 +1000
  Re: AttributeError Denis McMahon <denismfmcmahon@gmail.com> - 2015-08-12 11:36 +0000
  Re: AttributeError MRAB <python@mrabarnett.plus.com> - 2015-08-12 16:09 +0100
  Re: AttributeError Gene Heskett <gheskett@wdtv.com> - 2015-08-12 17:25 -0400
  Re: AttributeError Ltc Hotspot <ltc.hotspot@gmail.com> - 2015-08-12 15:02 -0700
  Re: AttributeError Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-08-12 23:16 +0100

csiph-web