2012-11-12 79 views
0

我有一個腳本可以生成10行,每行都在不同的行上。 我希望它生成10列,而不是行。python csv寫入列而不是行

import csv 
    import os 
    import random 
    for dirname, dirnames, filenames in os.walk(r"C:/Users/s/Desktop/filme/"): 
     for subdirname in dirnames: 
     foldere_filme = os.path.join(dirname, subdirname) 
     numarul_folderelor = foldere_filme.replace("C:/Users/s/Desktop/filme/", "") 
     print numarul_folderelor #1,2,3,4,5... 
     root_text = "C:/Users/s/Desktop/text/tags"+numarul_folderelor+".csv" 
     fisier_text_tags = csv.writer(open(root_text,'wb')) 
     for filenames in os.listdir(foldere_filme): 
      tags = filenames.replace("<SP>", " ").replace(".avi", "") 
      test = ['blah '+varible+' test, blah '+varible+' aaaaah clip, blah '+varible+' putlocker, download '+varible+' clip', 
        'blah '+varible+' free tuttiing, blah '+varible+' clip free, blah '+varible+' test clip, download '+varible+' aaaaah clip', 
        'blah '+varible+' 2012, blah '+varible+' test free, download '+varible+' 2012 clip, free tuttiing '+varible+'', 
        'blah '+varible+' free aaaaah clip, blah '+varible+' test 2012, where to blah '+varible+', '+varible+' aaaaah clip test', 
        'strem '+varible+' test free, blah '+varible+' free 2012, blah '+varible+' test for free, '+varible+' aaaaah clip test', 
        'putlocker 2012, blah '+varible+' free, download '+varible+' test, blah test '+varible+' for free, free clip tuttiing'] 
      random.shuffle(test) 
      fisier_text_tags.writerow(test[0:1]) 
+0

這幾乎就像有些人沒有聽說過評論和組織的重要性。 –

回答

0
import csv 
    import os 
    import random 
    for dirname, dirnames, filenames in os.walk(r"C:/Users/s/Desktop/filme/"): 
     for subdirname in dirnames: 
     foldere_filme = os.path.join(dirname, subdirname) 
     numarul_folderelor = foldere_filme.replace("C:/Users/s/Desktop/filme/", "") 
     print numarul_folderelor #1,2,3,4,5... 
     root_text = "C:/Users/s/Desktop/text/tags"+numarul_folderelor+".csv" 
     fisier_text_tags = csv.writer(open(root_text,'wb')) 

     row_test = [] 

     for filenames in os.listdir(foldere_filme): 
      tags = filenames.replace("<SP>", " ").replace(".avi", "") 
      test = ['blah '+varible+' test, blah '+varible+' aaaaah clip, blah '+varible+' putlocker, download '+varible+' clip', 
        'blah '+varible+' free tuttiing, blah '+varible+' clip free, blah '+varible+' test clip, download '+varible+' aaaaah clip', 
        'blah '+varible+' 2012, blah '+varible+' test free, download '+varible+' 2012 clip, free tuttiing '+varible+'', 
        'blah '+varible+' free aaaaah clip, blah '+varible+' test 2012, where to blah '+varible+', '+varible+' aaaaah clip test', 
        'strem '+varible+' test free, blah '+varible+' free 2012, blah '+varible+' test for free, '+varible+' aaaaah clip test', 
        'putlocker 2012, blah '+varible+' free, download '+varible+' test, blah test '+varible+' for free, free clip tuttiing'] 
      random.shuffle(test) 

      row_test.append(test[0]) 

     fisier_text_tags.writerow(row_test) 
+0

非常感謝你.. –

0

可以爲了傳遞一個迭代到writerow寫一些列:

writer.writerow(['hello', 'world']) 

該代碼會產生這個CSV文件時,寫入一個文件:

hello,world 
+0

謝謝你,我試過這個。謝謝。 –