Path: csiph.com!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'mrab': 0.05; 'data:': 0.07; 'f.close()': 0.07; 'cc:addr:python-list': 0.09; 'nameerror:': 0.09; 'snippet': 0.09; 'splitting': 0.09; 'jan': 0.11; 'wed,': 0.15; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'produces': 0.22; 'strip': 0.22; 'defined': 0.23; 'this:': 0.23; 'header:In-Reply- To:1': 0.24; 'define': 0.27; 'message-id:@mail.gmail.com': 0.27; 'whitespace': 0.29; 'print': 0.30; 'url:mailman': 0.30; 'code': 0.30; 'url:python': 0.33; 'sat': 0.33; 'open': 0.33; 'url:listinfo': 0.34; 'file': 0.34; 'handle': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'fri': 0.35; 'url:org': 0.36; 'skip:{ 10': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; '12,': 0.37; 'url:mail': 0.40; 'subject:skip:A 10': 0.63; '--->': 0.84; 'denis': 0.84; 'dict()': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=DztnbU9O+BykqXwnfcI/dN5vBzfa+1xXwWt+TsBGZ/Y=; b=jrmoq01Qlsyy9WJIi+q3GaSlpzLYHtBGGCSb0l94rJwZ5jdbPCqva+VtDqWiooduT0 kYYb6i4aIVaMePN+unv14okFZQODkU5qbz0O9uzL6IZdL6SM3hROAZjL2XFt36Mj/Xrl to5jl47JlXwaDFHKPoYN7mJPvkc9QszcmlN/aHtKqadMN8e/Jx5K451XSn5oUjwHMR7w 29YRIcymP9fxTeqmoKPq1dS9o2d0R089tv9U4tk05c3v5RIOUTIUXTkSYLPVIz91dNgs Eky13WUVkPPqe90ytlLRFyfh5QO0V/t4iG2pCjgZQfIMyHevWXk2OLazuelgjGW55EPU YvNQ== MIME-Version: 1.0 X-Received: by 10.152.219.3 with SMTP id pk3mr33819950lac.114.1439420755439; Wed, 12 Aug 2015 16:05:55 -0700 (PDT) In-Reply-To: <55CBCA4D.1010706@mrabarnett.plus.com> References: <0baa3bd5-9f80-4d4e-9367-84e2a32d8c70@googlegroups.com> <55CA92BA.7070905@mrabarnett.plus.com> <103ea014-9bdb-4c37-acd1-cdc64f5f721e@googlegroups.com> <3f132c37-bdb5-4313-9285-69e094392ccf@googlegroups.com> <55CB7823.8010402@mrabarnett.plus.com> <55CB815B.8070800@mrabarnett.plus.com> <5a09701f-06d3-42dd-aa96-59920de76044@googlegroups.com> <55CBCA4D.1010706@mrabarnett.plus.com> Date: Wed, 12 Aug 2015 16:05:55 -0700 Subject: Re: AttributeError From: Ltc Hotspot To: MRAB Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439420763 news.xs4all.nl 2829 [2001:888:2000:d::a6]:53742 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95325 On Wed, Aug 12, 2015 at 3:35 PM, MRAB 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