我想插入一個字符串到列表中。因爲我想這如何連接python中的列表?
TypeError: can only concatenate list (not "tuple") to list
:
我得到這個錯誤
var1 = 'ThisIsAString' # My string I want to insert in the following list
file_content = open('myfile.txt').readlines()
new_line_insert = file_content[:10] + list(var1) + rss_xml[11:]
open('myfile.txt', 'w').writelines(new_line_insert)
的myfile.txt的內容保存在 「FILE_CONTENT」 爲列表。 我要插入的10日線後的字符串VAR1,這就是爲什麼我做
file_content[:10] + list(var1) + rss_xml[11:]
但名單(VAR1)不起作用。我怎樣才能使這個代碼工作? 謝謝!
請您談一下在列表中插入,但你必須FILE_CONTENT [:10 ]和rss_xml [11:]。首先,這些是兩個不同的列表(在這裏我假設rss_xml *是一個列表),其次,你會錯過第10個元素。 – 2010-01-14 17:50:27