如何在包含/ in python的list元素中添加引号

时间:2017-06-29 13:31:32

标签: python

我有以下列表

a = [ documents/17361862_1455215234500329_3000435910753279754_n.jpg,  documents/17022254_1431360773552442_8479986202582740765_n_gqbi7GG.jpg]

我希望实现以下

[ "documents/17361862_1455215234500329_3000435910753279754_n.jpg",  "documents/17022254_1431360773552442_8479986202582740765_n_gqbi7GG.jpg"] 

我试过这个

x= ['{0}'.format(i) for i in a]

print x

但是我收到以下错误

 File "list.py", line 3

    a = [ documents/17361862_1455215234500329_3000435910753279754_n.jpg,  documents/17022254_1431360773552442_8479986202582740765_n_gqbi7GG.jpg] 
                                                                  ^
SyntaxError: invalid syntax

如何添加引号才能获得

[ "documents/17361862_1455215234500329_3000435910753279754_n.jpg",  "documents/17022254_1431360773552442_8479986202582740765_n_gqbi7GG.jpg"] 

2 个答案:

答案 0 :(得分:0)

如果您要在引号中打印字符串,可以执行以下操作之一:

"ASDFG 'those will show' Asdfg"
'ASDFG "Those will show" Asdfg'
"Asdfg \"Those will show\" Asdfg"

如果您打算将项目转换为字符串,请执行以下操作:

str(YOUR OBJECT)

也许您的代码存在点位置问题。这通常表示一个类的属性。

答案 1 :(得分:0)

如果要在列表中添加引号,可以执行以下操作:

a = [ "\"documents/17361862_1455215234500329_3000435910753279754_n.jpg\"", "\"documents/17022254_1431360773552442_8479986202582740765_n_gqbi7GG.jpg\""]

如果我错了,我很抱歉。