How to enter escape sequences in python

I need to add escape sequences to a string for certain characters (as an example here, a double quote is used). For example, if I have the line abra"cada"bra , I need to generate this: abra\"cada\"bra . But if the string already has escape characters for my interested literals (for example, a double quote in this example) abra\"cada\"bra , I need to leave it alone. What is the easiest way to do this in python?

(The idea is to write it to a text file that is read by another utility.)

+4
source share
5 answers

Probably the easiest way is to simply decode the string first so that nothing is escaped, and then output the resulting string again.

+2
source

You can get it with a corresponding negative look behind the statement in regular expressions:

 import re PAT = re.compile(r'(?<!\\)"') txt1 = '"abra"cada"bra' txt2 = '\\"abra\\"cada\\"bra' print PAT.sub(r'\\"', txt1) print PAT.sub(r'\\"', txt2) 

This ensures that it even works correctly if the quote is the first character of the sting, as in the example above.

+1
source

something like that

 def esc_string(mystring, delim, esc_char='\\'): return (esc_char+delim).join([s[:-1] if s.endswith(esc_char) else s for s in mystring.split(delim)]) 

then

 print esc_string('abra"cada"bra', '"') abra\"cada\"bra print esc_string('abra\\"cada\\"bra', '"') abra\"cada\"bra print esc_string('"boundary test"', '"') \"boundary test\" print esc_string('\\"boundary test\\"', '"') \"boundary test\" 
+1
source

Assuming that \ has no special meaning, except immediately before certain characters (for example, '"' ), then the @chepner clause for unescape can first be implemented as:

 def escape(text, char='"', escape="\\"): escaped_char = escape + char text = text.replace(escaped_char, char) # unescape return text.replace(char, escaped_char) # escape 

Enter

 "abra"cada"bra\" \"abra\"cada\"bra" "abra\"cada"bra\" abra\"cada\\"bra\" abra\"cada\\\"bra\" 

Output

 \"abra\"cada\"bra\" \"abra\"cada\"bra\" \"abra\"cada\"bra\" abra\"cada\\"bra\" abra\"cada\\\"bra\" 
+1
source

Regular expressions will do this. This means that it matches the character if it is not preceded by a backslash. I used "r" at the beginning of lines to tell python not to specifically treat the "\" character, and I had to translate it twice into tell the regular expression parser not to use it specifically. Try to help (re) for what (?

 import re re.sub(r'(?<!\\)"', r'\"', 'abra"cada\\"bra') # Returns 'abra\\"cada\\"bra' 
0
source

All Articles