line error on no. 7

Jean-Michel Pichavant jeanmichel at sequans.com
Wed Jul 29 16:23:20 CEST 2015


----- Original Message ----- 
> From: "ltc hotspot" <ltc.hotspot at gmail.com>
> To: "python-list at python.org" <Python-list at python.org>
> Sent: Tuesday, 28 July, 2015 10:21:59 PM
> Subject: line error on no. 7
> 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

Hi,


It's better to post the code with your question.

Python 2.7

fname = raw_input("Enter file name: ")
fh = open(fname)
lst = list()
for line in fh:
    if fh == list: continue
    list.split()
    list.append
    sorted("fh") 
print line.rstrip()

The main issue is that you've misspelled 'lst' in the for loop. You've used 'list' instead. 'list' is a built-in constructor for lists. You cannot split it. I'm also not sure what """ if fh == list:continue """ is supposed to achieve.

Try the following untested code:

words = []
for line in open(fname): # what if fname does not exist ?
    words.extend(line.split())
print sorted(words)
      
Regards,

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.


More information about the Python-list mailing list