2017-06-29 15 views
-3

我有以下列表如何添加引號列出包含元/ 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"] 
+0

您是否嘗試過直接在腳本中添加它們? – Tbaki

+0

我已經嘗試過,但它不工作,我的問題是一樣的這一個https://stackoverflow.com/questions/16054870/how-to-convert-list-into-string-with-quotes-in-python但只有數據是不同的,而不是mylist = [1,2,3],我的看起來像mylist = [documents/17361862_1455215234500329_3000435910753279754_n.jpg,documents/17022254_1431360773552442_8479986202582740765_n_gqbi7GG.jpg] – felix

+0

它不是,1,2,3是int,在python中是合法的sytaxe。您應該在腳本中編寫'mylist = [「documents/17361862_1455215234500329_3000435910753279754_n.jpg」,「documents/17022254_1431360773552442_8479986202582740765_n_gqbi7GG.jpg」]',您只能將這些字符作爲字符串傳遞。 – Tbaki

回答

0

如果你的意思是打印報價串標誌着您可以執行下列操作之一:

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

如果您打算轉換該項目到你做的字符串:

str(YOUR OBJECT) 

也許你的代碼有點問題,你的點位置。這通常表示一個類的屬性。

+0

感謝您的回覆,我的問題類似於https://stackoverflow.com/questions/16054870/how-to-convert-list-into-string-with-quotes-in-python,而不是mylist = [1,2,3],我會看起來像mylist = [文件/ 17361862_n.jpg,文件/ 1702gqbi7GG.jpg],謝謝你冰冷的電視 – felix

+0

但要添加引號,你需要的數據是一個字符串吧?你可以這樣做: 因爲我在MYDATA: RES + = 「 '{}'」 格式(I) 或者: RES = [] 因爲我在MYDATA: res.append( 「 '{}'」格式(i)) –

+0

非常感謝你幫助我冰冷的電視 – felix

0

如果你想在你的列表中添加引號,你可以這樣做:

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

我很抱歉,如果我錯了。

+0

謝謝我的問題是類似於這個https://stackoverflow.com/questions/16054870/how-to-convert-list-into-string-with -quotes-in-python但我的數據有點不同,而不是mylist = [1,2,3]數據看起來像mylist = [文檔/ 17361862_1455215234500329_3000435910753279754_n.jpg,文檔/ 17022254_1431360773552442_8479986202582740765_n_gqbi7GG.jpg],在此先感謝 – felix

相關問題