Path: csiph.com!news.mixmin.net!feeder1.xsusenet.com!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'data:': 0.07; 'f.close()': 0.07; 'val': 0.07; 'cc:addr:python-list': 0.09; 'lst': 0.09; 'nameerror:': 0.09; 'snippet': 0.09; 'splitting': 0.09; 'timestamp': 0.09; 'jan': 0.11; 'index': 0.13; 'wed,': 0.15; 'file,': 0.15; 'thu,': 0.15; '":"': 0.16; 'handle:': 0.16; 'hh:mm:ss': 0.16; 'line.split()': 0.16; 'wrote:': 0.16; 'tells': 0.18; 'programmer': 0.18; 'input': 0.18; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'aug': 0.20; 'posted': 0.21; 'are.': 0.22; 'assumes': 0.22; 'pos': 0.22; 'am,': 0.23; 'appears': 0.23; 'defined': 0.23; 'absolute': 0.23; 'elements': 0.23; 'insert': 0.23; 'split': 0.23; 'words': 0.24; 'header:In- Reply-To:1': 0.24; 'define': 0.27; 'message-id:@mail.gmail.com': 0.27; 'time:': 0.27; 'values': 0.28; 'actual': 0.28; 'record': 0.29; '-0700,': 0.29; '13,': 0.29; 'key,': 0.29; 'solution,': 0.29; 'print': 0.30; 'code': 0.30; 'post': 0.31; 'returned': 0.32; 'getting': 0.33; 'common': 0.33; 'sat': 0.33; 'traceback': 0.33; 'open': 0.33; 'file': 0.34; 'handle': 0.34; 'add': 0.34; 'lists': 0.34; 'received:google.com': 0.35; 'text': 0.35; 'follows:': 0.35; 'fri': 0.35; 'replace': 0.35; 'there': 0.36; 'lines': 0.36; 'data.': 0.36; 'subject:: ': 0.37; 'represent': 0.38; 'why': 0.39; 'data': 0.39; 'your': 0.60; 'subject:skip:A 10': 0.63; 'positions': 0.64; 'hours': 0.65; 'explanations': 0.72; '--->': 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=EHWmw9wuFYQbgIoRfDm+9+bQeT8BG5CNg7H7tf0ejjU=; b=SH5mzwTeWU+rvUE8HDwaBW1idCGgj8JPBPJ4d6OzxTrvviBFK2SZ6p+8G4DmXipZrd RZCKptbY1DKvNAuCBHzkHSIIIOBmlsBLWpf24oCNNscaDH5cdstp7UDDWc3LmzCZiy0I mvcmHDLcWxg1Dvsj99k4zr7otNYEpswD74sWf9hWPScBJR3XflYoTAhB4cQO0msxL2Nr xpPkAQLWdQKDw8jedCZ7rz9w8eZxAFGngi1ZQWshfRanKT5UW0weQiMtfVvrJU1tpCMT UtsW8AW9HT/wNwf6t61V05KPLPkQofC6Zaw5GYmdIHmmikpqUMYs2pRN+qceHNG1oXtC CYJw== MIME-Version: 1.0 X-Received: by 10.152.10.97 with SMTP id h1mr35907451lab.45.1439458915364; Thu, 13 Aug 2015 02:41:55 -0700 (PDT) In-Reply-To: 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> <55CBD3DE.2080407@mrabarnett.plus.com> Date: Thu, 13 Aug 2015 02:41:55 -0700 Subject: Re: AttributeError From: Ltc Hotspot To: Denis McMahon 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1439458918 news.xs4all.nl 2879 [2001:888:2000:d::a6]:58784 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95341 On Thu, Aug 13, 2015 at 2:15 AM, Denis McMahon 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