问题
I want to ignore the escape character in the following code.
>>> a=['\%']
>>> print a
['\\%']
I want to output like ['\%']
. Is there any way to do that?
回答1:
Using string_escape
, unicode_escape
encoding (See Python Specific Encodings):
>>> a = ['\%']
>>> print str(a).decode('string_escape')
['\%']
>>> print str(a).decode('unicode_escape')
['\%']
回答2:
Couple of manual ways:
>>> a=['\%']
>>> print "['{}']".format(a[0])
['\%']
>>> print "['%s']" % a[0]
['\%']
Or more generally:
>>> a=['\%', '\+']
>>> print '[{}]'.format(', '.join("'{}'".format(i) for i in a))
['\%', '\+']
>>> print '[%s]' % ', '.join("'%s'" % i for i in a)
['\%', '\+']
来源:https://stackoverflow.com/questions/20658032/how-to-ignore-the-escaping-python-list