[英]How to count a line number with specific word from fixed number of lines?
By using the below code, I can only count the number of FBC word and/or how many FBC is there. 通过使用以下代码,我只能计算FBC字数和/或有多少FBC。 But, I want to count a line number with specific word from fixed number of line
但是,我想用固定行数来计算具有特定字的行号
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)
Below is the data which I want to read from a text file: 下面是我想从文本文件中读取的数据:
-----------------------------------------------------------------------------
`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,`
At first, I want to read first 10 FBC-word containing the line
. 首先,我想阅读
10 FBC-word containing the line
前10 FBC-word containing the line
。 Among them, I want to count that line number which contains first non-zero FBC. 其中,我想计算包含第一个非零FBC的行号。 And, repeat the process for next
10 FBC-word containing the line
. 并且,重复
10 FBC-word containing the line
下一个10 FBC-word containing the line
的过程。
According to the given data & my query, the answer should be - 2, 3 Because the 2nd line contains first non-zero FBC for first 10 FBC-word containing line
AND the 3rd line contain first non-zero FBC for last 10 FBC-word containing line
根据给定的数据和我的查询,答案应该是 - 2,3,因为第二行包含前
10 FBC-word containing line
的第一个非零FBC,第三行包含最后10 FBC-word containing line
FBC的第一个非零10 FBC-word containing line
Unfortunately, I do not know how to do this using Python . 不幸的是,我不知道如何使用Python来做到这一点。 Please help me on this.
请帮帮我。
it is hard to understand what's the question about. 很难理解什么是问题。 simplify your input to for example:
简化您的输入,例如:
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,
and write what output do you want to get? 并写下你想得到什么输出?
[edited:] so you should read all the lines, extract only these with FBC statement and then find the index of the first line containing FBC but not FBC=0 [编辑:]所以你应该阅读所有的行,只用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)
you can that make operations on the list of boolean quite easily to find another indexes 你可以很容易地在布尔列表上进行操作以找到另一个索引
note that these indexes are valued from 0
, so the second one has index 1
请注意,这些索引的值从
0
,因此第二个索引的索引为1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.