A
A
AlmazKayum2020-07-31 19:33:38
Python
AlmazKayum, 2020-07-31 19:33:38

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

2 answer(s)
T
Ternick, 2020-07-31
@AlmazKayum

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.

M
milssky, 2020-07-31
@milssky

It could be shielding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question