python csv写入列而不是行

时间:2012-11-12 10:21:11

标签: python csv row

我有一个生成10行的脚本,每行都在不同的行上。 我希望它生成10列,而不是行。

    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])

2 个答案:

答案 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)

答案 1 :(得分:0)

您可以将迭代传递给writerow以编写多个列:

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

该代码在写入文件时将生成此csv文档:

hello,world
相关问题