简体   繁体   English

打印日志文件的特定行 - Python

[英]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; 如果我想更具体一点: if "Trace log : {"request":{"http"":然后打印跟踪日志内容; getting an error. 得到一个错误。

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

SyntaxError: invalid syntax SyntaxError:语法无效

any help would be greatly appreciated. 任何帮助将不胜感激。

@Maroun answered this is in a comment, I am writing an answer for future reference: @Maroun回答这是在评论中,我正在写一个答案以供将来参考:

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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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