9

我在 python 的文件中寻找一些单词。找到每个单词后,我需要从文件中读取接下来的两个单词。我一直在寻找一些解决方案,但我找不到只阅读接下来的单词。

# offsetFile - file pointer
# searchTerms - list of words

for line in offsetFile:
    for word in searchTerms:
        if word in line:
           # here get the next two terms after the word

感谢您的时间。

更新:只需要第一次出现。实际上,在这种情况下,该词只可能出现一次。

文件:

accept 42 2820 access 183 3145 accid 1 4589 algebra 153 16272 algem 4 17439 algol 202 6530

词:['访问','代数']

当我遇到“访问”和“代数”时搜索文件,我分别需要 183 3145 和 153 16272 的值。

4

4 回答 4

18

解决这个问题的一种简单方法是使用生成器读取文件,该生成器一次从文件中生成一个单词。

def words(fileobj):
    for line in fileobj:
        for word in line.split():
            yield word

然后找到您感兴趣的单词并阅读接下来的两个单词:

with open("offsetfile.txt") as wordfile:
    wordgen = words(wordfile)
    for word in wordgen:
        if word in searchterms:   # searchterms should be a set() to make this fast
            break
    else:
        word = None               # makes sure word is None if the word wasn't found

    foundwords = [word, next(wordgen, None), next(wordgen, None)]

现在foundwords[0]是您找到的单词,foundwords[1]是之后的单词,foundwords[2]是之后的第二个单词。如果没有足够的单词,则列表的一个或多个元素将是None.

如果您想强制它仅在一行内匹配,这会稍微复杂一些,但通常您可以将文件视为只是一个单词序列。

于 2012-04-22T01:37:27.420 回答
1
    def searchTerm(offsetFile, searchTerms):
            # remove any found words from this list; if empty we can exit
            searchThese = searchTerms[:]
            for line in offsetFile:
                    words_in_line = line.split()
                    # Use this list comprehension if always two numbers continue a word.
                    # Else use words_in_line.
                    for word in [w for i, w in enumerate(words_in_line) if i % 3 == 0]:
                            # No more words to search.
                            if not searchThese:
                                    return
                            # Search remaining words.
                            if word in searchThese:
                                    searchThese.remove(word)
                                    i = words_in_line.index(word)
                                    print words_in_line[i:i+3]

对于“访问”、“代数”,我得到以下结果:

['访问','183','3145']
['代数','153','16272']

于 2012-04-22T11:49:19.140 回答
1

如果您只需要检索前两个单词,只需执行以下操作:

offsetFile.readline().split()[:2]
于 2012-04-22T01:40:04.390 回答
1
word = '3' #Your word
delim = ',' #Your delim

with open('test_file.txt') as f:
    for line in f:
        if word in line:
            s_line = line.strip().split(delim)
            two_words = (s_line[s_line.index(word) + 1],\
            s_line[s_line.index(word) + 2])
            break
于 2012-04-22T01:47:42.747 回答