I am working on a web application.
I need to exit % and { } , if they exist, to further replace the string with .format() or %s
%
{ }
.format()
%s
I tried urllib quote_plus , re.escape() but nobody works.
urllib quote_plus
re.escape()
The line I need to execute is not static.
How can I solve this problem?
Thanks.
For use with % :
s = s.replace('%', '%%')
For use with format :
format
s = s.replace('{', '{{').replace('}', '}}')
To exit % , { and } . You can do this with the re.sub method. To go beyond string.format :
{
}
re.sub
string.format
re.sub(r'({|})', '\g<1>\g<1>', original)
To go beyond string % args :
string % args
re.sub(r'(%)', '\g<1>\g<1>', original)