简体   繁体   English

将单词从文本文件添加到列表(python 2.7)

[英]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.

相关问题 Python 2.7-在文本文件的列表中添加和删除项目 - Python 2.7 - add and remove items from a list in a text file 在Python 2.7中的Tkinter文本widgit中搜索单词列表 - Searching for a list of words within a Tkinter text widgit in Python 2.7 使用关键字作为Python 2.7中的起点从文件中检索文本块? - Retrieve block of text from a file, using key-words as start-point in Python 2.7? 将元组列表(来自 itertools)转换为文本文件中的单词列表,Python - Convert a list of tuples (from itertools) into a list of words in a text file, Python 从熊猫列中的列表中删除单词-python 2.7 - removing words from a list from pandas column - python 2.7 PYTHON(2.7):列表理解-在带有其他文本的.txt文件中添加(和)数字 - PYTHON(2.7): List comprehension - Adding(sum) numbers in a .txt file with additional text 从文本文件中计算特定单词的列表-Python - Counting a list of specific words from text file-Python 将文本文件中的唯一单词添加到 Python 中的列表中 - Add unique words from a text file to a list in python 如何从python中的文本文件返回单词列表 - How to return a list of words from a text file in python 列出Python中文本文件中每行的第一个单词 - List the first words per line from a text file in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM