Soundarya Thiagarajan
Soundarya Thiagarajan

Reputation: 574

Printing specific lines of log file - Python

I want to print specific content of the file from the log.. for each line in the file - printing the whole line. instead of th

with open(sys.argv[1], 'r') as f:   
    for line in f:  
       if "Trace log :" in line: 
          print line

This works fine.

If I want to be more specific about the line : if "Trace log : {"request":{"http"": then print the trace log content; getting an error.

if "Trace log : {"request":{"http"":
                        ^

SyntaxError: invalid syntax

any help would be greatly appreciated.

Upvotes: 1

Views: 460

Answers (1)

Caridorc
Caridorc

Reputation: 6641

@Maroun answered this is in a comment, I am writing an answer for future reference:

A string may not contain the quotation used to start it:

>>> 'foo' # OK
'foo'
>>> 'fo'' # Starts with ' and has ' inside, NOT OK
SyntaxError: EOL while scanning string literal

Either use another starting quote or escape the inside quote:

>>> "fo'o"
"fo'o"
>>> 'fo\'o'
"fo'o"

Upvotes: 3

Related Questions