1

I want to read a text file and extract each word from all lines to make a list of strings like below:

['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']

I wrote this code:

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

when I sort it in the end it gives nothing but a None. I get this unexpected result!

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

I am totally lost. What I am doing wrong?

2
  • What is the format of the text file? Commented May 12, 2016 at 21:42
  • Its a plane text file.But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief Commented May 12, 2016 at 21:43

4 Answers 4

3

.split() returns a list. So you are appending the returned list to lst. Instead you want to concat the 2 lists:

lst += line.split()

.sort() sorts the array in place, and does not return the sorted array. You can either use

print sorted(lst)

or

lst.sort()
print lst
Sign up to request clarification or add additional context in comments.

3 Comments

An alternative to the concatenation operator is the equivalent list.extend().
But then lst.sort() gives nothing but None.
@Mehmood, .sort() sorts your list in place, and does not return the sorted list
3

Use extend instead of append,

lst = list()

fname = raw_input("Enter file name: ")
with open(fname) as fh:
    for line in fh:
        lst.extend(line.rstrip.split()) # `rstrip` removes trailing whitespace characters, like `\n`

print(lst)
lst.sort() # Sort the items of the list in place
print(lst)

Python - append vs. extend

  • append: Appends object at end.
  • extend: Extends list by appending elements from the iterable.

Comments

1

Read the entire file with file.read() and split that string wherever there is whitespace with str.split():

with open(raw_input("Enter file name: "), 'r') as f:
    words = f.read().split()
print words
print sorted(words)

3 Comments

I am limited to use only split(), append() and sort() commands.
@Mehmood you said you were limited in the functions you can use, but then you posted an answer that completely copied my method. What gives?
You didn't use append() method, rather you used nice programming skills, which I hope to achieve this mastery some day. I need to write the code in a simple way using above mentioned commands, The programm should also exclude repetitive strings in the list.
0

Finally, I got it. Here is what I want.

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

1 Comment

set is faster. That is all you need, print(sorted(set(open(fname).read().split())))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.