Python converts double slashes \\ to single slashes \

Some time ago, due to the limitation of front-end technology, some of the parameters passed to me were single slashes (for example: \t) and some were double slashes (for example: \\t)

So the backend is needed to cooperate to convert double slashes into single slashes,

then:

a = 'asdf\\tsdfasd'
a = a.replace('\\\\','\\')

conversion failed,

After trying various methods, and then:

a = 'asdf\\tsdfasd'
a = eval(repr(a).replace('\\\\', '\\'))

Conversion successful! !

Little knowledge: The repr() function converts the object into a form that can be read by the interpreter

              The eval() function is used to execute a string expression and return the value of the expression. I often use it to process lists or dictionaries in string format. Direct eval (object) converts the format into a list or dictionary, which is very good use

 

Reprint please indicate the source, thank you!

Guess you like

Origin blog.csdn.net/MasterD56/article/details/102951306