AttributeError

MRAB python at mrabarnett.plus.com
Thu Aug 13 01:56:42 CEST 2015


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 at uct.ac.za Sat Jan  5 09:14:16 2008
> >From louis at 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.



More information about the Python-list mailing list