How to ignore the escaping \ python list?

人走茶凉 提交于 2019-12-10 19:56:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!