Counting jump(no of lines) between first two 'String' occurrences in a file

前端 未结 4 1062
天命终不由人
天命终不由人 2021-01-22 09:27

I have a huge data file with a specific string being repeated after a defined number of lines.

counting jump between first two \'Rank\' occurrences. For example the file

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-22 09:57

    counting jump between first two 'Rank' occurrences:

    def find_jumps(filename):
        first = True
        count = 0
        with open(filename) as f:
            for line in f:
                if 'Rank' in line:
                    if first:
                        count = 0 
                        #set this to 1 if you want to include one of the 'Rank' lines.
                        first = False                    
                    else:
                        return count
                else:
                    count += 1 
    

提交回复
热议问题