[英]Read text file line by line and Count specific word with particular values from a line
The below data is saved in a text file. 以下数据保存在文本文件中。 I can read that file line by line.
我可以逐行读取该文件。 Now, I want to start counting the "FBC = " and want to stop counting when it contains some specific value and save the line above "FBC".
现在,我要开始计算“ FBC =”,并希望在它包含某些特定值时停止计数,并在“ FBC”上方保存行。
Block = 150 块= 150
Erase time= 1830, Cycle= 0 擦除时间= 1830,周期= 0
Read time= 1617, Cycle= 1,DFFFFFFF,FFBBFFFF,FFFFFF8F,FDFBFFFF, 读取时间= 1617,周期= 1,DFFFFFFF,FFBBFFFF,FFFFFF8F,FDFBFFFF,
Page = 9600 FBC = 265, 页面= 9600 FBC = 265,
Read time= 1624, Cycle= 1,DFFFBFFF,FFBBFFFF,FFFFFF8F,FDFBFFFF, 读取时间= 1624,周期= 1,DFFFBFFF,FFBBFFFF,FFFFFF8F,FDFBFFFF,
Page = 9600 FBC = 355, 页面= 9600 FBC = 355,
Read time= 1623, Cycle= 1,CCFFBFFF,CCBBFFFF,CCFFFF8F,CCFBFFCC, 读取时间= 1623,周期= 1,CCFFBFFF,CCBBFFFF,CCFFFF8F,CCFBFFCC,
Page = 9600 FBC = 505, 页面= 9600 FBC = 505,
Read time= 1624, Cycle= 1,DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFFF, 读取时间= 1624,周期= 1,DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFFF,
Page = 9600 FBC = 642, 页面= 9600 FBC = 642,
Read time= 1617, Cycle= 1,DFFFBFFF,FFBBFFFC,FCFFFF8F,FDFBFFFF, 读取时间= 1617,周期= 1,DFFFBFFF,FFBBFFFC,FCFFFF8F,FDFBFFFF,
Page = 9600 FBC = 718, 页面= 9600 FBC = 718,
Block = 150 块= 150
Erase time= 1830, Cycle= 0 擦除时间= 1830,周期= 0
Read time= 1617, Cycle= 1,DFFFFFFF,FFBBFFFF,FFEFFF8F,FDFBFFFF, 读取时间= 1617,周期= 1,DFFFFFFF,FFBBFFFF,FFEFFF8F,FDFBFFFF,
Page = 9600 FBC = 235, 页面= 9600 FBC = 235,
Read time= 1624, Cycle= 1,DFFFFFFC,FFBBFFFF,FFEFFF8F,FDFBFFFF, 读取时间= 1624,周期= 1,DFFFFFFC,FFBBFFFF,FFEFFF8F,FDFBFFFF,
Page = 9600 FBC = 310, 页面= 9600 FBC = 310,
Read time= 1623, Cycle= 1,DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFFFF, 读取时间= 1623,周期= 1,DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFFFF,
Page = 9600 FBC = 445, 页面= 9600 FBC = 445,
Read time= 1624, Cycle= 1,DDFFFFFC,DDBBFFFB,DDEFFF8F,DDFBFFDD, 读取时间= 1624,周期= 1,DDFFFFFC,DDBBFFFB,DDEFFF8F,DDFBFFDD,
Page = 9600 FBC = 565, 页面= 9600 FBC = 565,
Read time= 1617, Cycle= 1,DFF7FFFC,FFBBFFFB,FFEFFF8F,FDFBFFBF, 读取时间= 1617,周期= 1,DFF7FFFC,FFBBFFFB,FFEFFF8F,FDFBFFBF,
Page = 9600 FBC = 680, 页面= 9600 FBC = 680,
Please help me to count `FBC` with particular values.
Please note, there will be more sections like these.
I tried the below-mentioned code, which can only count the Total FBC (Like 10). My expected result is also included below.
filename="Test.txt";
data_page=READ_DATA(filename);
def READ_DATA(filename):
infile = open(filename,'r') # open file for reading
lines = (line.rstrip() for line in infile)
lines = (line for line in lines if line)
FBC=[];
count =0;Flag=0;
for line in lines:
#if line.find("FBC = ") != -1:
if line.find("FBC = ") >= 500:
count=count+1;
print count
Expected output: it should stop counting when `FBC > 500`. So, for 1st Section, output will be `count = 3` and save `[CCFFBFFF,CCBBFFFF,CCFFFF8F,CCFBFFCC,]`.
For 2nd section, output will be `count = 4` and save `[DDFFFFFC,DDBBFFFB,DDEFFF8F,DDFBFFDD,]`.
Use the following approach: 使用以下方法:
import re
with open('test.txt') as f:
count, found = 0, False
pat = re.compile(r'\bFBC\s*=\s*(\d+),\s*(.+)')
for line in f:
line = line.strip()
if line:
if line.startswith('section_'):
count, found = 0, False
elif 'FBC' in line and not found:
count += 1
num, part = pat.search(line).groups()
if int(num) >= 500:
found = True
print('count', count)
print('line to save', part) # do save/write logic
If there's no section_
lines - use the following condition instead if line.startswith('Block'):
如果没有
section_
行, if line.startswith('Block'):
使用以下条件if line.startswith('Block'):
Sample output (for your current input text): 示例输出(针对您当前的输入文本):
count 3
line to save DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFFF,
count 4
line to save DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFFBF,
Problem Solved. 问题解决了。 Thanks to my friend & @RomanPerekhrest:
感谢我的朋友和@RomanPerekhrest:
with open('Test.txt') as f:
for line in f:
line = line.strip()
if line:
if line.startswith('Block'):
Rcount, found = 0, False
elif 'Read time' in line and not found:
readLine = line
Rcount += 1
nextLine = next(f)
if 'FBC' in nextLine and not found:
num = pat.search(nextLine).groups()
num = ''.join(map(str, num))
if int(num) >= 500:
found = True
print Rcount
print readLine
TotalCount.append(count)
O/P: O / P:
3 3
Read time= 1623, Cycle= 1,DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFCC, 读取时间= 1623,周期= 1,DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFCC,
4 4
Read time= 1624, Cycle= 1,DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFDDD, 读取时间= 1624,周期= 1,DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFDDD,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.