mGm
mGm

Reputation: 262

convert a text from a file to a list of strings in python

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?

Upvotes: 1

Views: 119

Answers (4)

mGm
mGm

Reputation: 262

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)  

Upvotes: 0

SparkAndShine
SparkAndShine

Reputation: 18007

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.

Upvotes: 3

Fabricator
Fabricator

Reputation: 12772

.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

Upvotes: 3

pzp
pzp

Reputation: 6597

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)

Upvotes: 1

Related Questions