Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #95263 > unrolled thread
| Started by | Ltc Hotspot <ltc.hotspot@gmail.com> |
|---|---|
| First post | 2015-08-11 17:01 -0700 |
| Last post | 2015-08-12 23:16 +0100 |
| Articles | 20 on this page of 46 — 10 participants |
Back to article view | Back to comp.lang.python
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
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-08-12 22:45 +0100 |
| Message-ID | <mailman.141.1439415947.3627.python-list@python.org> |
| In reply to | #95311 |
On 12/08/2015 22:15, Clayton Kirkwood wrote: > I'm thinking we are being played by a Turing type machine. > crk > Will you please stop top posting, how many times do people have to be asked until the message gets through? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-08-12 22:51 +0100 |
| Message-ID | <mailman.142.1439416324.3627.python-list@python.org> |
| In reply to | #95311 |
On 12/08/2015 22:32, Emile van Sebille wrote: > > @ltc -- read this -- then re-read all the responses you've been given. > Repeat ad nauseam. Until you can understand and incorporate advice > given you're wasting your time and ours asking. > This question has been asked on *THREE* different Python mailing lists. Do you really expect him to take any notice? I certainly don't, he got plonked by me days ago on the tutor mailing list, I just wish everone else would do the same so we could get rid of him, same as Nick the Greek and the RUE. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Ltc Hotspot <ltc.hotspot@gmail.com> |
|---|---|
| Date | 2015-08-12 14:04 -0700 |
| Message-ID | <mailman.163.1439456521.3627.python-list@python.org> |
| In reply to | #95311 |
So calling people stupid and ignorant on the internet makes you sexual arousal and to masturbate with yourself On Wed, Aug 12, 2015 at 1:38 PM, Denis McMahon <denismfmcmahon@gmail.com> wrote: > On Wed, 12 Aug 2015 12:05:37 -0700, Ltc Hotspot wrote: > >>>Have a look at assignment_10_2_v_06.py. > >> What should I look at assignment_10_2_v_06.py.: > > You shouldn't. You should instead approach your tutor and tell him you > are too stupid to learn computer programming[1], and can you please > transfer to floor-scrubbing 101. > > [1] You have repeatedly ignored advice and instructions that you have > been given. This is de-facto proof that you are not capable of learning > to program computers. > > -- > Denis McMahon, denismfmcmahon@gmail.com > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-08-13 10:37 +0100 |
| Message-ID | <mailman.164.1439458683.3627.python-list@python.org> |
| In reply to | #95311 |
On 12/08/2015 22:04, Ltc Hotspot wrote: > So calling people stupid and ignorant on the internet makes you sexual > arousal and to masturbate with yourself *plonk* - please follow suit everybody, it's quite clear that he has no interest in bothering with any of the data we've all provided. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2015-08-13 21:08 +1000 |
| Message-ID | <mailman.17.1439620562.4764.python-list@python.org> |
| In reply to | #95311 |
Ltc Hotspot <ltc.hotspot@gmail.com> writes: > So calling people stupid and ignorant on the internet makes you sexual > arousal and to masturbate with yourself With that, you have worn out your welcome here. Please don't post here again until you can refrain from puerile demeaning insults. -- \ “Alternative explanations are always welcome in science, if | `\ they are better and explain more. Alternative explanations that | _o__) explain nothing are not welcome.” —Victor J. Stenger, 2001-11-05 | Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-08-12 21:04 +0100 |
| Message-ID | <mailman.135.1439409891.3627.python-list@python.org> |
| In reply to | #95305 |
On 2015-08-12 20:05, Ltc Hotspot wrote:
> >Have a look at assignment_10_2_v_06.py.
>
> What should I look at assignment_10_2_v_06.py.:
How "time" is defined!
>
> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
> >From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
> """.split("\n")
> # Snippet file data: mbox-short.txt
>
> count = dict()
> #fname = raw_input("Enter file name: ")# Add Snippet file
> #handle = open (fname, 'r')# Add Snippet file
> for line in handle:
> if line.startswith("From "):
> time = line.split() # Sort time
>
> hours = line.split(":")[5] # Sort hours
> line = line.rstrip()
>
> count[hours] = count.get(hours, 0) + 1 # counter
>
> lst = [(val,key) for key,val in count.items()]
>
> print key, val
>
> On Wed, Aug 12, 2015 at 11:59 AM, MRAB <python@mrabarnett.plus.com> wrote:
> > On 2015-08-12 19:35, Ltc Hotspot wrote:
> >>
> >> Emile
> >>
> >> How do I define time in the revised code ?
> >>
> >>
> > Have a look at assignment_10_2_v_06.py.
> >
> >>
> >>
> >> ---------------------------------------------------------------------------
> >> Traceback Message reads:
> >> In [66]: %run assignment_10_2_v_07
> >>
> >> NameError
> >> Traceback (most recent call last)
> >> C:\Users\vm\Desktop\apps\docs\Python\week_10\assignment_10_2_v_07.py in
> >> <module>
> >> ()
> >> 9 for line in handle:
> >> 10 if line.startswith("From "):
> >> ---> 11 for key, val in time.split()[5]:
> >> 12 for key, val in hours.split(':')[0]:
> >> 13 count[hours] = count.get(hours, 0) + 1 # counter
> >>
> >> NameError: name 'time' is not defined
> >>
> >> ---------------------------------------------------------------------------
> >>>>>
> >>>>>
> >> In [67]: print time
> >> 0
> >>
> >> ---------------------------------------------------------------------------
> >>
> >> Revised Code reads:
> >> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
> >>>
> >>> From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
> >>
> >> """.split("\n")
> >> # Snippet file data: mbox-short.txt
> >>
> >> count = dict()
> >> #fname = raw_input("Enter file name: ")# insert snippet file
> >> #handle = open (fname, 'r')# insert snippet file
> >> for line in handle:
> >> if line.startswith("From "):
> >> for key, val in time.split()[5]:
> >> for key, val in hours.split(':')[0]:
> >> count[hours] = count.get(hours, 0) + 1 # counter
> >>
> >> lst = [(val,key) for key,val in count.items()]
> >> lst.sort(reverse=True)
> >>
> >> for key, val in lst[:12] :
> >> print key, val
> >>
> >>
> >> ---------------------------------------------------------------------------
> >> Regards,
> >> Hal
> >>
> > I'm baffled as to why you now have a nested 'for' loop 3 levels deep!
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
>
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-08-12 21:16 +0000 |
| Message-ID | <mqgd3p$8np$5@dont-email.me> |
| In reply to | #95305 |
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
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-08-12 23:35 +0100 |
| Message-ID | <mailman.145.1439418961.3627.python-list@python.org> |
| In reply to | #95315 |
On 2015-08-12 22:16, Denis McMahon wrote:
[snip]
> 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])
>
There's no need to strip whitespace just before splitting on it.
This:
l.strip().split()
can be shortened to this:
l.split()
[toc] | [prev] | [next] | [standalone]
| From | Ltc Hotspot <ltc.hotspot@gmail.com> |
|---|---|
| Date | 2015-08-12 16:05 -0700 |
| Message-ID | <mailman.148.1439420763.3627.python-list@python.org> |
| In reply to | #95315 |
On Wed, Aug 12, 2015 at 3:35 PM, MRAB <python@mrabarnett.plus.com> wrote:
> On 2015-08-12 22:16, Denis McMahon wrote:
> [snip]
>
>> 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])
>>
> There's no need to strip whitespace just before splitting on it.
>
> This:
>
> l.strip().split()
>
> can be shortened to this:
>
> l.split()
>
> --
> https://mail.python.org/mailman/listinfo/python-list
MRAB,
The Revised code produces a traceback:
()
9
10 c = [0 for i in range(24)]
---> 11 f = open(filename,'r')
12 for l in f:
13 h = int(l.split()[X].split(':')[Y])
NameError: name 'filename' is not defined
The revised code reads:
handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
>From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
""".split("\n")
# Snippet file data: mbox-short.txt
count = dict()
#fname = raw_input("Enter file name: ")# Add Snippet file
#handle = open (fname, 'r')# Add Snippet file
c = [0 for i in range(24)]
f = open(filename,'r')
for l in f:
h = int(l.split()[X].split(':')[Y])
c[h] = c[h] + 1
f.close()
for i in range(24):
print '{:02d} {}'.format(i, c[i])
How do I define the file name in order to remove the traceback?
Regards,
Hal
[toc] | [prev] | [next] | [standalone]
| From | Emile van Sebille <emile@fenx.com> |
|---|---|
| Date | 2015-08-12 16:15 -0700 |
| Message-ID | <mailman.149.1439421327.3627.python-list@python.org> |
| In reply to | #95315 |
On 8/12/2015 4:05 PM, Ltc Hotspot wrote:
> On Wed, Aug 12, 2015 at 3:35 PM, MRAB <python@mrabarnett.plus.com> wrote:
>> On 2015-08-12 22:16, Denis McMahon wrote:
>> [snip]
>>
>>> 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])
>>>
>> There's no need to strip whitespace just before splitting on it.
>>
>> This:
>>
>> l.strip().split()
>>
>> can be shortened to this:
>>
>> l.split()
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>
> MRAB,
>
> The Revised code produces a traceback:
> ()
> 9
> 10 c = [0 for i in range(24)]
> ---> 11 f = open(filename,'r')
> 12 for l in f:
> 13 h = int(l.split()[X].split(':')[Y])
>
> NameError: name 'filename' is not defined
So, read your posted code below and tell us where the label 'filename'
is defined. We can't find it. Neither can python. That's what the
error message means. Was that not clear? You do understand the
difference between text and labels, right? You have by now reviewed the
tutorial?
Emile
>
>
>
> The revised code reads:
>
> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
> From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
> """.split("\n")
> # Snippet file data: mbox-short.txt
>
> count = dict()
> #fname = raw_input("Enter file name: ")# Add Snippet file
> #handle = open (fname, 'r')# Add Snippet file
>
> c = [0 for i in range(24)]
> f = open(filename,'r')
> for l in f:
> h = int(l.split()[X].split(':')[Y])
> c[h] = c[h] + 1
> f.close()
> for i in range(24):
> print '{:02d} {}'.format(i, c[i])
>
>
> How do I define the file name in order to remove the traceback?
>
> Regards,
> Hal
>
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-08-13 00:16 +0100 |
| Message-ID | <mailman.150.1439421410.3627.python-list@python.org> |
| In reply to | #95315 |
On 2015-08-13 00:05, Ltc Hotspot wrote:
> On Wed, Aug 12, 2015 at 3:35 PM, MRAB <python@mrabarnett.plus.com> wrote:
> > On 2015-08-12 22:16, Denis McMahon wrote:
> > [snip]
> >
> >> 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])
> >>
> > There's no need to strip whitespace just before splitting on it.
> >
> > This:
> >
> > l.strip().split()
> >
> > can be shortened to this:
> >
> > l.split()
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
>
> MRAB,
>
> The Revised code produces a traceback:
> ()
> 9
> 10 c = [0 for i in range(24)]
> ---> 11 f = open(filename,'r')
> 12 for l in f:
> 13 h = int(l.split()[X].split(':')[Y])
>
> NameError: name 'filename' is not defined
>
>
>
> The revised code reads:
>
> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
> >From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
> """.split("\n")
> # Snippet file data: mbox-short.txt
>
> count = dict()
> #fname = raw_input("Enter file name: ")# Add Snippet file
> #handle = open (fname, 'r')# Add Snippet file
>
> c = [0 for i in range(24)]
> f = open(filename,'r')
> for l in f:
> h = int(l.split()[X].split(':')[Y])
> c[h] = c[h] + 1
> f.close()
> for i in range(24):
> print '{:02d} {}'.format(i, c[i])
>
>
> How do I define the file name in order to remove the traceback?
>
At this point I think I'll just let you figure that out for yourself...
[toc] | [prev] | [next] | [standalone]
| From | Ltc Hotspot <ltc.hotspot@gmail.com> |
|---|---|
| Date | 2015-08-12 16:46 -0700 |
| Message-ID | <mailman.151.1439423194.3627.python-list@python.org> |
| In reply to | #95315 |
>> How do I define the file name in order to remove the traceback?
>>
> At this point I think I'll just let you figure that out for yourself...
>
> --
> https://mail.python.org/mailman/listinfo/python-list
MRAB,
How do I define X?
-------------------------------------------------------------------------------------
Traceback reads:
10 f = open(filename,'r')
11 for l in f:
---> 12 h = int(l.split()[X].split(':')[Y])
13 c[h] = c[h] + 1
14 f.close()
NameError: name 'X' is not defined
-------------------------------------------------------------------------------------
Revised code reads:
handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
>From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
""".split("\n") # Snippet file data: mbox-short.txt
filename = raw_input("Enter file:")
if len(filename) < 1 : filename = "mbox-short.txt"
c = [0 for i in range(24)]
f = open(filename,'r')
for l in f:
h = int(l.split()[X].split(':')[Y])
c[h] = c[h] + 1
f.close()
for i in range(24):
print '{:02d} {}'.format(i, c[i])
Regards,
Hal
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-08-13 09:15 +0000 |
| Message-ID | <mqhn78$f58$1@dont-email.me> |
| In reply to | #95328 |
On Wed, 12 Aug 2015 16:46:32 -0700, Ltc Hotspot wrote:
> How do I define X?
>
-------------------------------------------------------------------------------------
> Traceback reads:
>
> 10 f = open(filename,'r')
> 11 for l in f:
> ---> 12 h = int(l.split()[X].split(':')[Y])
> 13 c[h] = c[h] + 1 14 f.close()
>
> NameError: name 'X' is not defined
If you read the text that I posted with the solution, it tells you what X
and Y are. They are numbers that describe the positions of elements in
your input data.
This absolute refusal by you to read any explanations that are posted are
exactly why you will never be a good programmer. To become a good
programmer you need to read and understand the explanations.
In the post with that code example, I wrote:
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.
You have to replace X and Y in that line with numbers that represent the
positions in the lists returned by the relevant split commands of the
actual text elements that you want to extract.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Ltc Hotspot <ltc.hotspot@gmail.com> |
|---|---|
| Date | 2015-08-13 02:41 -0700 |
| Message-ID | <mailman.165.1439458918.3627.python-list@python.org> |
| In reply to | #95339 |
On Thu, Aug 13, 2015 at 2:15 AM, Denis McMahon <denismfmcmahon@gmail.com> wrote:
> On Wed, 12 Aug 2015 16:46:32 -0700, Ltc Hotspot wrote:
>
>> How do I define X?
>>
> -------------------------------------------------------------------------------------
>> Traceback reads:
>>
>> 10 f = open(filename,'r')
>> 11 for l in f:
>> ---> 12 h = int(l.split()[X].split(':')[Y])
>> 13 c[h] = c[h] + 1 14 f.close()
>>
>> NameError: name 'X' is not defined
>
> If you read the text that I posted with the solution, it tells you what X
> and Y are. They are numbers that describe the positions of elements in
> your input data.
>
> This absolute refusal by you to read any explanations that are posted are
> exactly why you will never be a good programmer. To become a good
> programmer you need to read and understand the explanations.
>
> In the post with that code example, I wrote:
>
> 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.
>
> You have to replace X and Y in that line with numbers that represent the
> positions in the lists returned by the relevant split commands of the
> actual text elements that you want to extract.
>
> --
Denis,
What are the values of X & Y from the code as follows:
Code reads:
handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
>From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
""".split("\n") # snippet file data: mbox-short.txt
count = dict()
#fname = raw_input("Enter file name: ")# insert # to add snippet file data
#handle = open (fname, 'r')# insert # to add snippet file data
for line in handle:
if line.startswith("From "):
time = line.split() # splitting the lines ->
# print time: ['From', 'stephen.marquard@uct.ac.za', 'Sat',
'Jan', '5', '09:14:16', '2008']
for hours in time: #getting the index pos of time ->
hours = line.split(":")[2] # splitting on ":" ->
line = line.rstrip()
count[hours] = count.get(hours, 0) + 1 # getting the index pos of hours.
lst = [(val,key) for key,val in count.items()] # find the most common words
lst.sort(reverse=True)
for key, val in lst[:12] :
print key, val
Regards,
Hal
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-08-13 11:06 +0000 |
| Message-ID | <mqhtnc$f58$2@dont-email.me> |
| In reply to | #95341 |
On Thu, 13 Aug 2015 02:41:55 -0700, Ltc Hotspot wrote:
>>> How do I define X?
> What are the values of X & Y from the code as follows:
> # print time: ['From', 'stephen.marquard@uct.ac.za', 'Sat', 'Jan', '5',
'09:14:16', '2008']
This is the data you need to look at.
X is the position in the printed list of the time information. If you can
not determine the correct X value by inspection of the list having been
told which element it should be, then you need to go back to the python
documentation and read about the list data object and how elements within
the list are referenced. Once you understand from the documentation how
to reference the list elements, you will be able to determine by
inspection of the above list the correct value for X.
Y is the position of the hours element within the time information when
that information is further split using the ':' separator. You may need
to refer to the documentation for the split() method of the string data
object. Once you understand from the documentation how the string.split()
function creates a list, and how to reference the list elements (as
above), you will be able to determine by inspection the correct value for
Y.
This is fundamental python knowledge, and you must discover it in the
documentation and understand it. You will then be able to determine the
correct values for X and Y.
Note that the code I posted may need the addition of a line something
like:
if line.startswith("From "):
in a relevant position, as well as additional indenting to take account
of that addition.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2015-08-13 00:56 +0100 |
| Message-ID | <mailman.152.1439423806.3627.python-list@python.org> |
| In reply to | #95315 |
On 2015-08-13 00:46, Ltc Hotspot wrote:
> >> How do I define the file name in order to remove the traceback?
> >>
> > At this point I think I'll just let you figure that out for yourself...
> >
> > --
> > https://mail.python.org/mailman/listinfo/python-list
>
> MRAB,
>
> How do I define X?
> -------------------------------------------------------------------------------------
> Traceback reads:
>
> 10 f = open(filename,'r')
> 11 for l in f:
> ---> 12 h = int(l.split()[X].split(':')[Y])
> 13 c[h] = c[h] + 1
> 14 f.close()
>
> NameError: name 'X' is not defined
> -------------------------------------------------------------------------------------
> Revised code reads:
> handle = """From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
> >From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008
> """.split("\n") # Snippet file data: mbox-short.txt
>
> filename = raw_input("Enter file:")
> if len(filename) < 1 : filename = "mbox-short.txt"
>
>
> c = [0 for i in range(24)]
> f = open(filename,'r')
> for l in f:
> h = int(l.split()[X].split(':')[Y])
> c[h] = c[h] + 1
> f.close()
> for i in range(24):
> print '{:02d} {}'.format(i, c[i])
>
Print out the result of "l.split()" and figure out the value of X that
will give you the part you want.
Then, print out the result of the ".split(':')" part and figure out the
value of Y that will give you the part you want.
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-08-12 20:28 +0000 |
| Message-ID | <mqga8h$8np$3@dont-email.me> |
| In reply to | #95293 |
On Wed, 12 Aug 2015 09:29:50 -0700, Ltc Hotspot wrote: > Using the attached file of a diagram as a frame, why is there an > attribute message? Perhaps you should read the message. It's very clear. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-08-12 21:58 +0100 |
| Message-ID | <mailman.137.1439413205.3627.python-list@python.org> |
| In reply to | #95310 |
On 12/08/2015 21:28, Denis McMahon wrote: > On Wed, 12 Aug 2015 09:29:50 -0700, Ltc Hotspot wrote: > >> Using the attached file of a diagram as a frame, why is there an >> attribute message? > > Perhaps you should read the message. It's very clear. > Not to Ltc Hotspot. I'm unsure as to whether he's simply incompetent or a troll, but whatever I plonked him several days ago on the tutor mailing list. Since then he's asked the same question here and on the core mentorship list. I suggest that everybody stop wasting their time and bandwidth. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-08-12 03:46 +0100 |
| Message-ID | <mailman.95.1439347585.3627.python-list@python.org> |
| In reply to | #95263 |
On 12/08/2015 01:49, Chris Angelico wrote: > On Wed, Aug 12, 2015 at 10:43 AM, Ltc Hotspot <ltc.hotspot@gmail.com> wrote: >> Python can pull the hour from the 'From ' line by finding the time and then >> splitting the string a second time using a colon, i.e., From >> stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 >> >> Finally, accumulated the counts for each hour, print out the counts, sorted >> by hour as shown below: > > In that case, you want to sort the entire collection, not a single > key-value pair. > > It seems to me you can do this fairly efficiently with collections.Counter. > Which is exactly what the OP was told on the tutor mailing list several days ago, and he's also asked on the core mentorship list earlier today as well. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-08-12 04:32 +0100 |
| Message-ID | <mailman.97.1439350359.3627.python-list@python.org> |
| In reply to | #95263 |
On 12/08/2015 04:05, Ltc Hotspot wrote: > Chris, > > Check the code and the visualize execution of the code, available at > http://tinyurl.com/p8tgd5h > > message reads: NameError: name 'collections' is not defined > Which is why we kept telling you over on the tutor mailing list to show us your complete code, not to put it in a link, and also not to top post. Why we bother as volunteers to waste our time on someone such as yourself who posts mutiple times but refuses to take any notice of what actually gets said I really don't know. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
Page 2 of 3 — ← Prev page 1 [2] 3 Next page →
Back to top | Article view | comp.lang.python
csiph-web