简体   繁体   中英

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.

@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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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