AttributeError

Ltc Hotspot ltc.hotspot at gmail.com
Thu Aug 13 11:41:55 CEST 2015


On Thu, Aug 13, 2015 at 2:15 AM, Denis McMahon <denismfmcmahon at 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 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: ")# 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 at 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


More information about the Python-list mailing list