0

我有一个生成 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])
4

2 回答 2

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)
于 2012-11-12T10:27:21.213 回答
0

您可以传递一个可迭代对象writerow以写入多个列:

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

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

hello,world
于 2012-11-12T10:33:45.380 回答