[英]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. .split()
返回一个列表。 So you are appending the returned list to lst
. 因此,您要将返回的列表附加到
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. .sort()
对数组进行原位排序,而不返回排序后的数组。 You can either use 您可以使用
print sorted(lst)
or 要么
lst.sort()
print lst
Use extend
instead of append
, 使用
extend
而不是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 Python-追加与扩展
append
: Appends object at end. append
:在末尾追加对象。 extend
: Extends list by appending elements from the iterable. extend
:通过添加来自iterable的元素来扩展列表。 Read the entire file with file.read()
and split that string wherever there is whitespace with str.split()
: 阅读与整个文件
file.read()
和只要有空白与拆分字符串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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.