我有一段代碼將一堆行寫入csv文件。它寫道:我想要什麼,但是......它插入括號中的每一列... 的代碼是:Python csv寫入列表
root_text_csv = "C:/Users/s/Desktop/text/knife"+numarul_folderelor+".csv"
fisier_text_knife = csv.writer(open(root_text_csv,'wb'))
row_test = []
for filenames in os.listdir(foldere_prrra):
knife = filenames.replace("<SP>", " ").replace(".sys", "")
test = ['cut '+knife+' off for fuged,', 'cut '+knife+' off,', 'cut '+knife+' fool alabala,', 'cut '+knife+' nieh,', 'prrra '+knife+' alabala,',
'cut '+knife+' fuged streaming,', 'cut '+knife+' alabala fuged,', 'cut '+knife+' off alabala,', 'prrra '+knife+' fool alabala,',
'cut '+knife+' off fuged,', 'prrra '+knife+' niether alabala,', 'cut off '+knife+' for fuged,' 'cut '+knife+' fuged fool alabala,',
'cut '+knife+' off niether,', 'where to cut '+knife+',', ''+knife+' fool alabala off,', 'steel '+knife+' off fuged,', 'cut '+knife+' fuged niether,',
''+knife+' fool alabala off,', 'fuged streaming '+knife+',', 'cut '+knife+' niether,', 'cut '+knife+' fuged,', 'red '+knife+' off,', ''+knife+' off,',
'red '+knife+',', 'cut '+knife+',', ''+knife+' fuged,', 'fuged '+knife+',', ''+knife+' off,', 'off '+knife+',', ''+knife+',']
random.shuffle(test)
scris = [x for x in test if len(x) <= 30]
row_test.append(scris) #row_test.append(scris[0])
fisier_text_knife.writerow(row_test)
如果我row_test.append(scris[0])
更換row_test.append(scris[0:7])
它只寫串了他們所有的(但它寫入沒有括號 - > [] < - )。
我想寫7或8行。我在這段代碼上浪費了我的大腦。
謝謝你的時間。
'.extend()'會比'+ ='用片更好。 – geoffspear
'row_test + = scris [0:7]'而不是for循環之前的'row_test = []'?我不能那樣做......''row_test = []'是爲了寫作colomns。 –