How can I selectively escape percent (%) in Python

2019-01-01 04:14发布

问题:

I have the following code

test = \"have it break.\"
selectiveEscape = \"Print percent % in sentence and not %s\" % test

print(selectiveEscape)

I would like to get the output:

Print percent % in sentence and not have it break.

What actually happens:

    selectiveEscape = \"Use percent % in sentence and not %s\" % test
TypeError: %d format: a number is required, not str

回答1:

>>> test = \"have it break.\"
>>> selectiveEscape = \"Print percent %% in sentence and not %s\" % test
>>> print selectiveEscape
Print percent % in sentence and not have it break.


回答2:

Alternatively, as of Python 2.6, you can use new string formatting (described in PEP 3101):

\'Print percent % in sentence and not {0}\'.format(test)

which is especially handy as your strings get more complicated.



回答3:

try using %% to print % sign .



回答4:

You can\'t selectively escape %, as % always has a special meaning depending on the following character.

In the documentation of Python, at the bottem of the second table in that section, it states:

\'%\'        No argument is converted, results in a \'%\' character in the result.

Therefore you should use:

selectiveEscape = \"Print percent %% in sentence and not %s\" % (test, )

(please note the expicit change to tuple as argument to %)

Without knowing about the above, I would have done:

selectiveEscape = \"Print percent %s in sentence and not %s\" % (\'%\', test)

with the knowledge you obviously already had.



回答5:

If the formatting template was read from a file, and you cannot ensure the content doubles the percent sign, then you probably have to detect the percent character and decide programmatically whether it is the start of a placeholder or not. Then the parser should also recognize sequences like %d (and other letters that can be used), but also %(xxx)s etc.

Similar problem can be observed with the new formats -- the text can contain curly braces.



回答6:

I have tried different methods to print a subplot title, look how they work. It\'s different when i use Latex.

It works with \'%%\' and \'string\'+\'%\' in a typical case.

If you use Latex it worked using \'string\'+\'\\%\'

So in a typical case:

import matplotlib.pyplot as plt
fig,ax = plt.subplots(4,1)
float_number = 4.17
ax[0].set_title(\'Total: (%1.2f\' %float_number + \'\\%)\')
ax[1].set_title(\'Total: (%1.2f%%)\' %float_number)
ax[2].set_title(\'Total: (%1.2f\' %float_number + \'%%)\')
ax[3].set_title(\'Total: (%1.2f\' %float_number + \'%)\')

Title examples with %

If we use latex:

import matplotlib.pyplot as plt
import matplotlib
font = {\'family\' : \'normal\',
        \'weight\' : \'bold\',
        \'size\'   : 12}
matplotlib.rc(\'font\', **font)
matplotlib.rcParams[\'text.usetex\'] = True
matplotlib.rcParams[\'text.latex.unicode\'] = True
fig,ax = plt.subplots(4,1)
float_number = 4.17
#ax[0].set_title(\'Total: (%1.2f\\%)\' %float_number) This makes python crash
ax[1].set_title(\'Total: (%1.2f%%)\' %float_number)
ax[2].set_title(\'Total: (%1.2f\' %float_number + \'%%)\')
ax[3].set_title(\'Total: (%1.2f\' %float_number + \'\\%)\')

We get this: Title example with % and latex