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?

mGm
  • 264
  • 2
  • 12
  • What is the format of the text file? – pzp May 12 '16 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 – mGm May 12 '16 at 21:43

4 Answers4

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
Fabricator
  • 12,722
  • 2
  • 27
  • 40
  • An alternative to the concatenation operator is the equivalent [`list.extend()`](https://docs.python.org/2/tutorial/datastructures.html#more-on-lists). – pzp May 12 '16 at 21:41
  • But then lst.sort() gives nothing but None. – mGm May 12 '16 at 21:45
  • @Mehmood, `.sort()` sorts your list in place, and does not return the sorted list – Fabricator May 12 '16 at 21:48
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.
Community
  • 1
  • 1
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134
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)
pzp
  • 6,249
  • 1
  • 26
  • 38
  • I am limited to use only split(), append() and sort() commands. – mGm May 12 '16 at 22:12
  • @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? – pzp May 12 '16 at 22:52
  • 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. – mGm May 12 '16 at 23:26
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)  
mGm
  • 264
  • 2
  • 12