简体   繁体   中英

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?

.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

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.

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)

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)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM