[Baypiggies] line error on no. 7 (ltc.hotspot at gmail.com)

Andrew Francis andrew.francis at mail.mcgill.ca
Wed Jul 29 17:40:34 CEST 2015


Hi Hal:

I took a look at your code. Reading a line (which is a string) at a time from the file is okay. However
you want to do a strip() to get rid of line breaks, then do a split() to get the parsed word list. After than you 
want to add the parsed list to a list of all the words.

Keeping with the spirit of what you are doing, here is a sketch:

my_list = []
with open(some_file_name) as f:
    for line in f:
        my_list.extend(line.strip().split(" "))

my_list.sort()
print my_list

Cheers,
Andrew

Message: 1
Date: Tue, 28 Jul 2015 20:14:22 +0000
From: <ltc.hotspot at gmail.com>
To: "=?utf-8?Q?baypiggies at python.org?=" <baypiggies at python.org>
Subject: [Baypiggies] line error on no. 7
Message-ID: <55b7e31f.e67c460a.1bb8.255c at mx.google.com>
Content-Type: text/plain; charset="utf-8"

Hi Everyone,


I'm writing python code to read a data text file, split the file into a list of words using the split(function) and to print the results in alphabetical order.


The raw python code is located at http://tinyurl.com/oua9uqx


The sample data is located at
http://tinyurl.com/odt9nhe



Desired Output: ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder']


There is a line error on no. 7
What is the cause of this error?



Regards,
Hal






Sent from Surface
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20150728/e7e62667/attachment-0001.html>

------------------------------

Message: 2
Date: Tue, 28 Jul 2015 13:55:36 -0700 (PDT)
From: "Martin Falatic" <martin at falatic.com>
To: ltc.hotspot at gmail.com
Cc: "baypiggies at python.org" <baypiggies at python.org>
Subject: Re: [Baypiggies] line error on no. 7
Message-ID:
        <7267.24.7.60.89.1438116936.squirrel at martin-wwwss5.ssl.supercp.com>
Content-Type: text/plain;charset=iso-8859-1

First of all, you can't use split() on a list.

Second, you have lots of typos... you're calling things like "append" on
the data type, not the variable you created (list.append versus
lst.append(something). Read the man pages... e.g.,
https://docs.python.org/2/tutorial/datastructures.html

 - Marty



On Tue, July 28, 2015 13:14, ltc.hotspot at gmail.com wrote:
> Hi Everyone,
>
>
>
> I'm writing python code to read a data text file, split the file into a
> list of words using the split(function) and to print the results in
> alphabetical order.
>
>
> The raw python code is located at http://tinyurl.com/oua9uqx
>
>
>
> The sample data is located at
> http://tinyurl.com/odt9nhe
>
>
>
>
> Desired Output: ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and',
> 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light',
> 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what',
> 'window', 'with', 'yonder']
>
>
>
> There is a line error on no. 7
> What is the cause of this error?
>
>
>
>
> Regards,
> Hal
>
>
>
>
>
>
>
> Sent from Surface_______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> https://mail.python.org/mailman/listinfo/baypiggies




------------------------------

Message: 3
Date: Tue, 28 Jul 2015 15:00:04 -0700 (PDT)
From: "Martin Falatic" <martin at falatic.com>
To: ltc.hotspot at gmail.com
Cc: baypiggies at python.org
Subject: Re: [Baypiggies] line error on no. 7
Message-ID:
        <8595.24.7.60.89.1438120804.squirrel at martin-wwwss5.ssl.supercp.com>
Content-Type: text/plain;charset=iso-8859-1

(Keeping replies on the mailing list.)

I'm referring to the list data structure and its methods, like append()
(but *not* including split()).

I'm sure lst.append isn't doing much either without an argument.

What's the goal of this exercise? That will help illuminate what you're
trying to accomplish. Based on the original pastebin
(http://tinyurl.com/oua9uqx) this is what's happening:

fname = raw_input("Enter file name: ")
fh = open(fname) # Open a file
lst = list()  # Create a new list
for line in fh: # For each line in the file...
    if fh == list: continue  # Eh??
    list.split()  # Illegal (can't do this to a list and it's on a type
anyway)
    list.append   # Not appending anything, and it's on a type not a var too
    sorted("fh")  # You've sorted the string "fh" and assigned result to
nothing
print line.rstrip() # Finally, we only print the very last line we read.

I don't think that's the plan, but that's how the code parses so far.

 - Marty



On Tue, July 28, 2015 14:25, ltc.hotspot at gmail.com wrote:
> Marty,
>
>
> Are you referring under 5.1. Using Lists as Queues?
>
>
>
> 1st.append produced a new error message.
>
>
>
> Hal
>
>
>
> Sent from Surface
>
>
>
>
>
>
> From: Martin Falatic
> Sent: ???Tuesday???, ???July??? ???28???, ???2015 ???1???:???55??? ???PM
> To: ltc.hotspot at gmail.com
> Cc: baypiggies at python.org
>
>
>
>
>
>
> First of all, you can't use split() on a list.
>
>
> Second, you have lots of typos... you're calling things like "append" on
> the data type, not the variable you created (list.append versus
> lst.append(something). Read the man pages... e.g.,
> https://docs.python.org/2/tutorial/datastructures.html
>
>
> - Marty




------------------------------

Message: 4
Date: Tue, 28 Jul 2015 15:40:31 -0700
From: Simeon Franklin <simeonf at gmail.com>
To: ltc.hotspot at gmail.com
Cc: "baypiggies at python.org" <baypiggies at python.org>
Subject: Re: [Baypiggies] line error on no. 7
Message-ID:
        <CAK0d_GooKfkNFXocMU5cRzsh3_TecTFkVBDnKRuC5nuCn1wHfg at mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hey Hal -

The errors in your code seem to me to come from trying to run before you
can walk. Let me give you one example of what should be going through your
head:

>>> list.split()

Why doesn't this work? Well - you have to understand every individual piece
of this statement. Start with "list" - that's a builtin data type in
Python. The builtins are the things you automatically have access to
without importing. If you open a brand new python shell and type list
you'll see that list is already there!

$ python
>>> list
<type 'list'>

Next is the "." character. The dot is the attribute operator which allows
you to look inside the thing on the left for the thing on the right. The
thing on the left is the list type and the thing on the right is named
split. Lastly the line has parens "()" which is the call operator. This
implies that "split" is a thing in list and the kind of thing is something
you can call - a function or a method.

If I try to run this line of code I get an error:

>>> list.split()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'list' has no attribute 'split'

What this is telling me is that the list type has no attribute called
split. I could retrieve all the attributes by using another builtin "dir"
to verify that this is so:

>>> dir(list)
[... a bunch of  __ugly__ attributes and then... 'append', 'count',
'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

Aha! The list type has append, count, extend, index, insert, pop, remove,
reverse, sort methods. There is no split method on lists and that's why
this line of code crashes...

You might already know all this stuff but my impression is you are trying
to debug a program without understanding the basics - you don't know how to
solve your own problems yet because you don't have a firm understanding of
the basic syntax and concepts in python.

Don't feel bad! Everybody is there when they are learning a new language -
when I read Haskell code I spend a lot of time saying "I don't know have
any idea what this piece of punctuation even means!" But if you're not an
experienced developer it helps a lot to get a grounding in the basics of a
language before you try to solve problems with it.

I'd really recommend you try something like Learn Python the Hard Way - see
http://learnpythonthehardway.org/book/ for the free book. Work through each
chapter and don't skip any of the exercises. By the time you hit chapter 10
you ought to be able to complete the exercise you're working on right now
and understand each and every line...

I know it feels like a step back to review basic material instead of
working on your problem. But it might be more efficient in the long run -
I'm sure it feels frustrating to be blocked and waiting for email responses
from the list...

On Tue, Jul 28, 2015 at 1:14 PM, <ltc.hotspot at gmail.com> wrote:

>  Hi Everyone,
>
> I'm writing python code to read a data text file, split the file into a
> list of words using the split(function) and to print the results in
> alphabetical order.
>
> The raw python code is located at http://tinyurl.com/oua9uqx
>
> The sample data is located at
> http://tinyurl.com/odt9nhe
>
>
> Desired Output: ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and',
> 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light',
> 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window',
> 'with', 'yonder']
>
> There is a line error on no. 7
> What is the cause of this error?
>
>
> Regards,
> Hal
>
> Sent from Surface
>
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> https://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20150728/5ad77655/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Baypiggies mailing list
Baypiggies at python.org
To change your subscription options or unsubscribe:
https://mail.python.org/mailman/listinfo/baypiggies

------------------------------

End of Baypiggies Digest, Vol 117, Issue 6
******************************************


More information about the Baypiggies mailing list