Home python python " n" do not work in the string that returns from...

python “\ n” do not work in the string that returns from the database




Store in MongoDB database Different lines, for example: "ABC \ N ABC" when trying to output it through print (my_string), I get a conclusion: "ABC \ N ABC" instead of expected:


Here is the code:

my_string = get_one_string_from_bd ()
Print (My_String)
Print (Type (My_String))
Print (REPR (My_String))


& lt; class 'str' & gt;

What am I doing not as and how to make the row translation work?
The problem is solved if adding (n with two chopsticks, replaced by N with one stick, for some reason on this site is replaced by 1) .replace (“\ n”, “\ n”) to the string, but it is somehow strange.

Answer 1, Authority 100%

The database line does not contain a string translation character, it has a sequence of characters \ and N . The string was most likely converted to JS using JSON.Stringify .

In order to restore the spacecraft in the row, you can use

my_string = my_string.encode ('latin1'). Decode. Encode ('Latin1 ') .Decode (' UTF8 ')

and still

import codecs
My_String = Codecs.escape_Decode (My_String) [0] .Decode ('Unicode-Escape')

Sample code in Python 3

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions