AttributeError
Ltc Hotspot
ltc.hotspot at gmail.com
Wed Aug 12 19:05:55 EDT 2015
On Wed, Aug 12, 2015 at 3:35 PM, MRAB <python at 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 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
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
More information about the Python-list
mailing list