Escape Special Characters in a Python String

How to escape special characters of a string with single backslashes

This is one way to do it (in Python 3.x):

escaped = a_string.translate(str.maketrans({"-":  r"\-",
"]": r"\]",
"\\": r"\\",
"^": r"\^",
"$": r"\$",
"*": r"\*",
".": r"\."}))

For reference, for escaping strings to use in regex:

import re
escaped = re.escape(a_string)

Escape special characters in a Python string

Use re.escape

>>> import re
>>> re.escape(r'\ a.*$')
'\\\\\\ a\\.\\*\\$'
>>> print(re.escape(r'\ a.*$'))
\\\ a\.\*\$
>>> re.escape('www.stackoverflow.com')
'www\\.stackoverflow\\.com'
>>> print(re.escape('www.stackoverflow.com'))
www\.stackoverflow\.com

Repeating it here:

re.escape(string)

Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.

As of Python 3.7 re.escape() was changed to escape only characters which are meaningful to regex operations.

Escape Special Characters in Python 3.x

As of Python 3.7 re.escape() was changed to escape only characters which are meaningful to regex operations(that's way < and > are not escaped).

You can use the regex module as alternative:

pip install regex

Change your code to:

import regex
string = "->A.T.A<-BlackDrago"
escaped = regex.escape(string,special_only=False)
print(escaped)
>>> print(escaped)
\-\>A\.T\.A\<\-BlackDrago

How to escape special characters in string for Python 2.7

You don't need to escape values for the purpose of SQL by hand! Let the database API take care of that.

  1. Form a valid string literal in Python source code:

    str = "You're the best \"dog\" on earth."
    str = 'You\'re the best "dog" on earth.'
    str = """You're the best "dog" on earth."""

    These are all equivalent, you just need to escape the appropriate quotes that you're using as string literal terminators.

  2. Use the database API correctly and don't worry about escaping. From the manual:

    sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
    cursor.execute(sql, ('webmaster@python.org', 'very-secret'))

    Escaping is handled by separating the query and values, not by adding backslashes.

Python adds special characters to path-string

\ is escape char in Python. According to docs, you have created string with \f ASCII Formfeed (FF) character.

String literals can be enclosed in matching single quotes (') or
double quotes ("). They can also be enclosed in matching groups of
three single or double quotes (these are generally referred to as
triple-quoted strings). The backslash (\) character is used to escape
characters that otherwise have a special meaning, such as newline,
backslash itself, or the quote character.

Either use double slashes (\\ - to escape escape character) or use raw string literals (r"some\path").

String literals may optionally be prefixed with a letter 'r' or 'R';
such strings are called raw strings and use different rules for
interpreting backslash escape sequences.

How to escape special characters in Python?

You can use the built-in function html.unescape:

import html
df['Country/Region'] = df['Country/Region'].astype(str).map(html.unescape)

Output:

>>> df
Country/Region
0 Belgium (Dutch)
1 Saint Lucia
2 Trinidad and Tobago
3 Sierra Leone
4 Mali
5 Svalbard and Jan Mayen

Escape special HTML characters in Python

In Python 3.2, you could use the html.escape function, e.g.

>>> string = """ Hello "XYZ" this 'is' a test & so on """
>>> import html
>>> html.escape(string)
' Hello "XYZ" this 'is' a test & so on '

For earlier versions of Python, check http://wiki.python.org/moin/EscapingHtml:

The cgi module that comes with Python has an escape() function:

import cgi

s = cgi.escape( """& < >""" ) # s = "& < >"

However, it doesn't escape characters beyond &, <, and >. If it is used as cgi.escape(string_to_escape, quote=True), it also escapes ".



Here's a small snippet that will let you escape quotes and apostrophes as well:

 html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}

def html_escape(text):
"""Produce entities within text."""
return "".join(html_escape_table.get(c,c) for c in text)



You can also use escape() from xml.sax.saxutils to escape html. This function should execute faster. The unescape() function of the same module can be passed the same arguments to decode a string.

from xml.sax.saxutils import escape, unescape
# escape() and unescape() takes care of &, < and >.
html_escape_table = {
'"': """,
"'": "'"
}
html_unescape_table = {v:k for k, v in html_escape_table.items()}

def html_escape(text):
return escape(text, html_escape_table)

def html_unescape(text):
return unescape(text, html_unescape_table)


Related Topics



Leave a reply



Submit