Python 如何从固定行数中计算具有特定字的行号?

Python 如何从固定行数中计算具有特定字的行号?,python,full-text-search,Python,Full Text Search,通过使用下面的代码,我只能计算FBC单词的数量和/或有多少FBC。但是,我想从固定行数中计算一个具有特定单词的行号 def lcount(keyword, fname): with open(fname, 'r') as fin: return sum([1 for line in fin if keyword in line]) F=lcount('FBC', 'BLK100-199C1-J-1000-K-10.txt'); print (F)

通过使用下面的代码,我只能计算FBC单词的数量和/或有多少FBC。但是,我想从固定行数中计算一个具有特定单词的行号

    def lcount(keyword, fname):
    with open(fname, 'r') as fin:
        return sum([1 for line in fin if keyword in line])
    F=lcount('FBC', 'BLK100-199C1-J-1000-K-10.txt');
    print (F)
下面是我想从文本文件中读取的数据:

-----------------------------------------------------------------------------
`PagesPerBlock= 64                      
Block = 100                     
Read time= 698, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=0,

Read time= 697, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=2,

Read time= 697, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=3,

Read time= 698, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=4,

Read time= 691, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=11,

Read time= 698, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=20,

Read time= 697, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=32,

Read time= 691, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=45,

Read time= 698, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=54,

Read time= 691, Cycle= 1,55555555,55555555,55555555,    Page=6400   FBC=71,

PagesPerBlock= 64                       
Block = 101                     
Read time= 690, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=0,

Read time= 697, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=0,

Read time= 698, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=3,

Read time= 691, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=7,

Read time= 697, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=11,

Read time= 691, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=15,

Read time= 698, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=24

Read time= 698, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=34,

Read time= 698, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=42,

Read time= 697, Cycle= 1,55555555,55555555,55555555,    Page=6464   FBC=50,`

首先,我想先读包含行的10个FBC单词。其中,我想计算包含第一个非零FBC的行号。并且,对包含该行的下一个
10 FBC字重复该过程

根据给定的数据和我的查询,答案应该是-2,3 因为第二行包含第一个
10个包含行的FBC单词的第一个非零FBC,第三行包含最后一个
10个包含行的FBC单词的第一个非零FBC


不幸的是,我不知道如何使用Python实现这一点。请帮我解决这个问题。

很难理解这个问题是关于什么的。简化输入,例如:

header1
header2
some_data_n   FBC=0,
some_data_b   FBC=1,
some_data_v   FBC=2,
some_data_c   FBC=0,
some_data_x   FBC=3,
然后写下你想要得到什么输出


[已编辑:] 因此,您应该读取所有行,使用FBC语句仅提取这些行,然后找到包含FBC但不包含FBC=0的第一行的索引

def line_index(keyword, fname):
    with open(fname, 'r') as fin:
        # get all lines from file
        lines = fin.readlines()
        # get only lines with keyword
        lines = [ln for ln in lines if keyword in ln]
        # check where the keyword has value 0
        zero_value_str = "%s=%d" % (keyword, 0)
        presence = [zero_value_str in ln for ln in lines]

        # The first element where 0-valued FBC is not present
        index1 = presence.index(False)
        # Now we don't need this element so we switch the value for this index
        presence[index1] = True
        # now we search for the second
        index2 = presence.index(False)

        # We want to numerate indexes starting from 1, not 0, so increment them
        return index1 + 1, index2 + 1

F=line_index('FBC', 'BLK100-199C1-J-1000-K-10.txt');
print (F)
可以使布尔值列表上的操作非常容易地找到另一个索引


请注意,这些索引是从
0
开始计算的,因此第二个索引的索引
1

先生,我想读取前12行,其中有10个FBC。我想计算包含第一个非零FBC的行号(行号应该是2,其中包含第一个非零FBC),然后我想对最后12行重复这个过程(这里的行号应该是3,其中包含第一个非零FBC)。最后,结果应该是2,3.ok,编辑。适用于my python 2.7您需要编辑此部分
zero\u value\u str=“%s=%d”%(关键字,0)
才能使用python 3Dear先生,根据您的输入,我想知道包含第一个非零FBC的第一行号。对于您的输入,第一个非零FBC的行号是0或1,但我得到两个输出,如(2,3)。输出应为一个值,因为第二行包含第一个非零FBC[FBC=1]。请帮助我。所以你们需要从每个块中得到第一个非零值FBC的索引?然后,您应该将整个文件内容读入字符串,并按块头
PagesPerBlock
将其拆分,然后将该函数应用于每个块内容。不过我还是认为你应该把你的问题简化到一些更简单的数据上。例如,文件“BLK100-199C1-J-1000-K-10.txt”的名称在这里并不重要-您可以将其替换为
data.txt
,并更具体地说明要解决的问题。