Answer the question
In order to leave comments, you need to log in
Why doesn't \n in a Python string work when I fetch it from the db?
There is a line
string = 'HELLO\nWORLD'
If you declare it inside the script as above. Then the literal \n works like a newline.
If 'HELLO\nWORLD' is in the POSTGRESQL database of the VARCHAR type, then the string is displayed directly with the literal without a newline.
What can be wrong?
Answer the question
In order to leave comments, you need to log in
The point is that '\n' only has magic in PL. Otherwise, it's just two characters. In fact, at the stage of code processing, '\n' is replaced by a carriage return character {in theory, it is not visible}. Most likely you need to process the string somehow or shove it into a function like this:
def formatter(s):
return s.replace("\\n", "\n").replace("\\t", "\t")# and etc.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question