I do not really know how to say it, but when I raise exception in python 3.2, '\n' aren't parsed...
Here is an example:
class ParserError(Exception):
def __init__(self, message):
super().__init__(self, message)
try:
raise ParserError("This should have\na line break")
except ParserError as err:
print(err)
It works like this:
$ ./test.py
(ParserError(...), 'This should have\na line break')
How do I make sure new lines are printed as new lines?
class ParserError(Exception):
pass
or
print(err.args[1])
Ahh, err.message was deprecated in 2.6 - so no longer present, so...
print(err.args[1])
What's happening here is that the repr
of your message string is being printed as part of passing the whole Exception
object to print()
, so the newline is being converted back into \n
. If you individually print
the actual string, the actual newline will print.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With