Cheap way to search a large text file for a string

后端 未结 9 1082
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 04:15

I need to search a pretty large text file for a particular string. Its a build log with about 5000 lines of text. Whats the best way to go about doing that? Using regex sho

9条回答
  •  感情败类
    2020-11-27 04:54

    5000 lines isn't big (well, depends on how long the lines are...)

    Anyway: assuming the string will be a word and will be seperated by whitespace...

    lines=open(file_path,'r').readlines()
    str_wanted="whatever_youre_looking_for"
    
    
        for i in range(len(lines)):
            l1=lines.split()
            for p in range(len(l1)):
                if l1[p]==str_wanted:
                    #found
                    # i is the file line, lines[i] is the full line, etc.
    

提交回复
热议问题