Python re escaping raises TypeError: first argument must be string or compiled pattern
Python re escaping raises TypeError: first argument must be string or compiled pattern
我有几千行的错误日志,我想匹配所有出现的这些类型:
Traceback (most recent call last):
File "my_code.py", line 83, in upload_detection_image
put_response = s3_object.put(Body=image, ContentType="image/jpeg")
File "/usr/local/lib/python3.6/dist-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(*args, **params)
File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 676, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.
更一般地说,我想匹配一些以给定字符串开头并以另一个字符串结尾的文本。文本可以是多行的。问题是文本的开头和结尾可能包含一些可能需要转义的专用正则表达式字符。
我的尝试:
pattern = rf"Traceback(.*){re.escape('botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.')}",
re.findall(pattern, text, flags=re.MULTILINE)
它在 findall
上给我错误:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 2, in <module>
File "/Users/me/.pyenv/versions/3.6.12/lib/python3.6/re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
File "/Users/me/.pyenv/versions/3.6.12/lib/python3.6/re.py", line 300, in _compile
raise TypeError("first argument must be string or compiled pattern")
TypeError: first argument must be string or compiled pattern
谢谢
您的所有评论都有助于最终解决方案:
pattern = rf"Traceback(.*?){re.escape('botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.')}"
results = re.findall(pattern, text, flags=re.DOTALL)
我有几千行的错误日志,我想匹配所有出现的这些类型:
Traceback (most recent call last):
File "my_code.py", line 83, in upload_detection_image
put_response = s3_object.put(Body=image, ContentType="image/jpeg")
File "/usr/local/lib/python3.6/dist-packages/boto3/resources/factory.py", line 520, in do_action
response = action(self, *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/boto3/resources/action.py", line 83, in __call__
response = getattr(parent.meta.client, operation_name)(*args, **params)
File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 357, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/usr/local/lib/python3.6/dist-packages/botocore/client.py", line 676, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.
更一般地说,我想匹配一些以给定字符串开头并以另一个字符串结尾的文本。文本可以是多行的。问题是文本的开头和结尾可能包含一些可能需要转义的专用正则表达式字符。 我的尝试:
pattern = rf"Traceback(.*){re.escape('botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.')}",
re.findall(pattern, text, flags=re.MULTILINE)
它在 findall
上给我错误:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<string>", line 2, in <module>
File "/Users/me/.pyenv/versions/3.6.12/lib/python3.6/re.py", line 222, in findall
return _compile(pattern, flags).findall(string)
File "/Users/me/.pyenv/versions/3.6.12/lib/python3.6/re.py", line 300, in _compile
raise TypeError("first argument must be string or compiled pattern")
TypeError: first argument must be string or compiled pattern
谢谢
您的所有评论都有助于最终解决方案:
pattern = rf"Traceback(.*?){re.escape('botocore.exceptions.ClientError: An error occurred (ExpiredToken) when calling the PutObject operation: The provided token has expired.')}"
results = re.findall(pattern, text, flags=re.DOTALL)