[英]Adding words from a text file to a list (python 2.7)
Assuming I have a text file. 假设我有一个文本文件。
My goal is to write a function which receives a number of line to go over in the text file and returns a list, each cell in the list containing one word exactly from that line. 我的目标是编写一个函数,该函数接收文本文件中要经过的多行代码并返回一个列表,列表中的每个单元格都恰好包含该行中的一个单词。
Any idea of how doing this ? 知道怎么做这个吗?
thanks 谢谢
If you are working with small files: 如果您正在使用小文件:
def get_words(mifile, my_line_number):
with open(mifile) as f:
lines = f.readlines()
myline = lines[my_line_number] #first line is 0
return myline.split()
you get all the file lines in the list lines
. 您将获得列表
lines
中的所有文件lines
。 This is not very efficient for VERY big files. 对于非常大的文件,这不是很有效。 In that case probably it would be better to iterate line by line until you arrive to the chosen line.
在这种情况下,最好逐行进行迭代,直到到达所选择的行。
Given the filename
and the line number ( lineno
), you could extract the words on that line this way: 给定
filename
和行号( lineno
),您可以通过以下方式提取该行的单词:
Assuming the lineno
is not too large: 假设
lineno
不太大:
import linecache
line = linecache.getline(filename, lineno)
words = line.split()
Or, if the lineno
is large: 或者,如果
lineno
很大:
import itertools
with open(filename,'r') as f:
line = next(itertools.islice(f,lineno-1,None))
words = line.split()
This,of course,assumes that words are separated by spaces--which may not be the case in hard-to-parse text. 当然,这假设单词由空格分隔 - 在难以解析的文本中可能不是这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.