繁体   English   中英

逐行读取文本文件,并从一行中计算特定单词的特定值,并将该行保存在该行上方

[英]Read text file line by line and Count specific word for particular values from a line and save the line above that

以下数据保存在文本文件中。 现在,我要开始对“ FBC =”进行计数,并希望在它包含某些特定值时停止计数,并在该“ FBC”字上面保存行。

Block = 150

Erase time= 1830, Cycle= 0

Read time= 1617, Cycle= 1,FFFFFFFF,FFBBFFFF,FFFFFF8F,FDFBFFFF,

Page = 9600 FBC = 265, 

Read time= 1624, Cycle= 1,DFFFBFFF,FFBBFFFF,FFFFFF8F,FDFBFFFF,

Page = 9600 FBC = 355, 

Read time= 1623, Cycle= 1,DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFCC,

Page = 9600 FBC = 505, 

Read time= 1624, Cycle= 1,DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFDD,

Page = 9600 FBC = 642, 

Read time= 1617, Cycle= 1,DFFFBFFF,FFBBFFFC,FCFFFF8F,FDFBFFEE,

Page = 9600 FBC = 718, 

Block = 150

Erase time= 1830, Cycle= 0

Read time= 1617, Cycle= 1,DFFFFFFF,FFBBFFFF,FFEFFF8F,FDFBFAAA,

Page = 9600 FBC = 235, 

Read time= 1624, Cycle= 1,DFFFFFFC,FFBBFFFF,FFEFFF8F,FDFBFBBB,

Page = 9600 FBC = 310, 

Read time= 1623, Cycle= 1,DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFCCC,

Page = 9600 FBC = 445, 

Read time= 1624, Cycle= 1,DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFDDD,

Page = 9600 FBC = 565, 

Read time= 1617, Cycle= 1,DFF7FFFC,FFBBFFFB,FFEFFF8F,FDFBFFBF,

Page = 9600 FBC = 680, 

请帮助我计算具有特定值的FBC 请注意,将有更多类似的部分。

我尝试了下面提到的代码,而我得到的O / p是

3

4

['Read time= 1623', ' Cycle= 1', 'DFFFBFFF', 'FFBBFFFF', 'FCFFFF8F', 'FDFBFFCC', '']
['Read time= 1623', ' Cycle= 1', 'DFFFFFFC', 'FFBBFFFB', 'FFEFFF8F', 'FDFBFCCC', ''].

我的预期结果也包括在下面。

    with open('Test.txt') as f:
        count, found = 0, False
        pat = re.compile(r'\bFBC\s*=\s*(\d+)')
        P_Stress = []
        TotalCount = []
        for line in f:
            line1 = line.strip()
            if line1:
                if line1.startswith('Block'):
                    count, found = 0, False
                elif 'FBC' in line1 and not found:
                    count += 1
                    num = pat.search(line1).groups()
                    num = ''.join(map(str, num))
                    if int(num) >= 500:
                        found = True
                        print count            
    with open('Online_StackOverflow_2.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:
                    Rcount += 1
                    for i in range(0, len(TotalCount), 1):     
                        if Rcount==TotalCount[i]:
                            xx=line.split(",");
                            print xx
                        break;

预期输出:当FBC > 500时应停止计数。 因此,对于第一部分,输出将count = 3并保存DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFCC;对于第二部分,输出将count = 4并保存DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFDDD,。

问题解决了。 感谢我的朋友和@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:

3

读取时间= 1623,周期= 1,DFFFBFFF,FFBBFFFF,FCFFFF8F,FDFBFFCC,

4

读取时间= 1624,周期= 1,DFFFFFFC,FFBBFFFB,FFEFFF8F,FDFBFDDD,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM