簡體   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