I am writing a script to parse the logs. I need a regular expression to find the python exception in the log file, if any.
For example if below exception is in the log file, it should return below exception in string format.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'klj' is not defined
I tried below code, it won't work:
import urllib.request
import re
txt = urllib.request.urlopen(log_url).read().decode('utf-8')
exceptions = re.findall("(\w+)Error:", txt)
Thanks in Advance
Your regex was only looking for exception names that ended in 'Error', but many end in 'Exception'. And you were capturing in capture group 1 only the characters that preceded 'Error', so you would not get the full name.
The following regex will look only for exception names at the start of a line and capture the full name:
import re
txt = """Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in <module>
NameError: name 'klj' is not defined"""
exceptions = re.findall("""(?x) # Verbose mode
(?m) # Multiline mode (^ matches start of a line)
^ # Match start of a line
(?:\w+) # Match one or more word characters in a non-capturing group
(?:Error|Exception) # Match 'Error' or 'Exception':
(?=: ) # Lookahead assertion: next characters are ': '
""", txt)
print(exceptions)
Prints:
['NameError']
Related
I want to launch pybrain tests on Python3 but I get error:
Traceback (most recent call last):
File "runtests.py", line 107, in <module>
runner.run(make_test_suite())
File "runtests.py", line 72, in make_test_suite
test_package = __import__(test_package_path, fromlist=module_names)
File "B:\msys64\mingw64\bin\WinPython\Python373\lib\site-packages\pybrain\tests\__init__.py", line 1, in <module>
from helpers import gradientCheck, buildAppropriateDataset, xmlInvariance, \
File "B:\msys64\mingw64\bin\WinPython\Python373\Lib\site-packages\pybrain\tests\helpers.py", line 42
print 'Module has no parameters'
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print('Module has no parameters')?
I looked helpers.py and found that prints are without parents(as operators, I think it was in Python2).How can I fix that?Can I import some module to
execute with such problem, for example six, but I don t know what it does.
My Code:
Error:
$ C:/Users/abc/AppData/Local/Programs/Python/Python38-32/python.exe
e:/PYTHON/pdf_word_converter.py Traceback (most recent call last):
File "e:/PYTHON/pdf_word_converter.py", line 4, in
conout = pypandoc.convert_file("E:\PYTHON\0267_Docusign.pdf", "docx", outputfile= "readme.docx") File
"C:\Users\abc\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pypandoc__init__.py",
line 137, in convert_file
raise RuntimeError("source_file is not a valid path") RuntimeError: source_file is not a valid path
It is because of the escape sequence.
Two options.
Replace all \ with \\ in "E:\PYTHON\0267_Docusign.pdf"
Use raw string r"E:\PYTHON\0267_Docusign.pdf"
With Python 2, creating a Decimal with an invalid string produces a useful error message:
>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 547, in __new__
"Invalid literal for Decimal: %r" % value)
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/decimal.py", line 3872, in _raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: 'spam'
While Python 3 produces a not-so-helpful message:
>>> import decimal
>>> decimal.Decimal('spam')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
Is there any way to get a useful message like "Invalid literal for Decimal: 'spam'" from the exception in Python 3?
I'm using Python 2.7.15 and Python 3.7.2, both on darwin.
Addenda:
It looks like Python 2 once had a not-very-helpful message for decimal.InvalidOperation: https://bugs.python.org/issue1770009
This situation looks analogous but most of it goes over my head: https://bugs.python.org/issue21227
You could monkey-patch the decimal module.
import decimal
def safe_decimal(something):
try:
funct_holder(something)
except Exception as e:
new_errror = Exception("Hey silly that's not a decimal, what should I do with this? {}".format(something))
raise new_errror from None
funct_holder = decimal.Decimal
decimal.Decimal = safe_decimal
Then you could use the monkey patched version as so
>>> decimal.Decimal('hello')
Traceback (most recent call last):
File "<input>", line 12, in <module>
File "<input>", line 6, in safe_decimal
Exception: Hey silly that's not a decimal, what should I do with this? hello
Having assignment "Extracting Data With Regular Expressions". For this I'm importing regex, but the code is not working. what is my mistake?
I checked the code without "import", it does work. Lines 2-7 are working. But it got a traceback error on "import re" line 1.
import re
fname = input('Enter file: ')
if len(fname) < 1 : fname = "sample.txt"
hand = open(fname)
hd = hand.read()
for line in hand:
line = line.rstrip()
nm = re.findall('[0-9]+',line)
print(nm)
C:\Users\Desktop\new>re.py
Enter file:
Traceback (most recent call last):
File "C:\Users\Desktop\new\re.py", line 1, in <module>
import re
File "C:\Users\Desktop\new\re.py", line 9, in <module>
[enter image description here][1]nm = re.findall('[0-9]+',line)
AttributeError: module 're' has no attribute 'findall'
Because you have called your file re.py, the import will actually import this file instead of the built-in module for regular expressions.
Just rename your file to something different and it should work as expected.
I am using Python 3.3 through the IDLE. While running a code that looks like:
raise KeyError('This is a \n Line break')
it outputs:
Traceback (most recent call last):
File "test.py", line 4, in <module>
raise KeyError('This is a \n Line break')
KeyError: 'This is a \n Line break'
I would like it to output the message with the line break like this:
This is a
Line Break
I have tried to convert it to a string before or using os.linesep but nothing seems to work. Is there any way I can force the message to be correctly shown on the IDLE?
If I raise an Exception (instead of KeyError) then the output is what I want, but I would like to still raise a KeyError if possible.
You problem has nothing to do with IDLE. The behavior you see is all from Python. Running current repository CPython interactively, from a command line, we see the behavior you reported.
Python 3.7.0a2+ (heads/pr_3947:01eae2f721, Oct 22 2017, 14:06:43)
[MSC v.1900 32 bit (Intel)] on win32
>>> raise KeyError('This is a \n Line break')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'This is a \n Line break'
>>> s = 'This is a \n Line break'
>>> s
'This is a \n Line break'
>>> print(s)
This is a
Line break
>>> raise Exception('This is a \n Line break')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
Exception: This is a
Line break
>>> raise IndexError(s)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: This is a
Line break
>>> try:
... raise KeyError('This is a \n Line break')
... except KeyError as e:
... print(e)
'This is a \n Line break'
>>> try:
... raise KeyError('This is a \n Line break')
... except KeyError as e:
... print(e.args[0])
This is a
Line break
I don't know why KeyError acts differently from even IndexError, but printing e.args[0] should work for all exceptions.
EDIT
The reason for the difference is given in this old tracker issue, which quotes a comment in the KeyError source code:
/* If args is a tuple of exactly one item, apply repr to args[0].
This is done so that e.g. the exception raised by {}[''] prints
KeyError: ''
rather than the confusing
KeyError
alone. The downside is that if KeyError is raised with an
explanatory
string, that string will be displayed in quotes. Too bad.
If args is anything else, use the default BaseException__str__().
*/
This section appears in the KeyError_str object definition in Objects/exceptions.c of the Python source code.
I will mention your issue as another manifestation of this difference.
There is a way to get the behavior you want: Simply subclass str and override __repr__:
class KeyErrorMessage(str):
def __repr__(self): return str(self)
msg = KeyErrorMessage('Newline\nin\nkey\nerror')
raise KeyError(msg)
Prints:
Traceback (most recent call last):
...
File "", line 5, in
raise KeyError(msg)
KeyError: Newline
in
key
error