use variable inside regex python

前端 未结 1 1485
夕颜
夕颜 2021-01-16 16:01

It\'s worth leaving here that this is my second day in Python and I\'m not very pro at this language. Any low level suggestion and easy to understand would be much appreciat

相关标签:
1条回答
  • 2021-01-16 16:41

    You can't reference a variable in a string. A string is just text, it has no knowledge of the namespace, and the interpreter does not resolve that for it.

    Since your variable dia is a string, you can just use that in your call to re.findall:

    if re.findall(dia, line):
        pass
    

    or something similar:

    if re.findall(r"{0}".format(dia), line):
        pass
    

    As for that correctness of what you're doing, if format of the time stamp on the log is the same what you're using, it should be correct.

    EDIT: if you are reading strings from the log, you need not (or shouldn't) open them as binary, i.e. the b flag

    0 讨论(0)
提交回复
热议问题