如何让 python 读取 csv 文件并提取数据并将其粘贴到另一个 csv?

How to make python read csv files and extract data and paste it another csv?

而不是使用 output.csv 如果我想让类别列表中的单词成为文件 name.I 尝试过但没有用。

import csv

handle_w = open('output.csv', 'wb')
csv_out = csv.writer(handle_w, delimiter=' ')

csv_out.writerow(["  ", "Waybill", "Contents", "  ", "Amount", "COD Amount", "Type", "  ", "Error"])

with open('ss.csv') as handle2:
    for each in handle2:
        j = each
        print j
        strng_conv = ''.join(map(str,j))
        file_name = strng_conv+'.csv'
        print file_name
        cat_file = open(file_name,'wb')
        cat_var =csv.writer(cat_file,delimiter=' ')


        with open('1.csv', 'rb') as csvfile:
            handle1 = csv.reader(csvfile, delimiter=' ')
            for row in handle1:
                if each.rstrip() in row:

                    cat_var.writerow(row)
        cat_file.close()
handle_w.close()

如果我理解你的问题,首先你需要阅读这两个文件。第二个ss.csv很简单,建议不要用csv库来阅读。

简单

import csv

def open_writer(tag):
    handle_w = open('%s.csv' %(tag, ), 'wb')
    return handle_w, csv.writer(handle_w, delimiter=' ')

handle0 = open('ss.csv', 'r')
writers = map(open_writer, [tag.rstrip() for tag in handle0])

handle0.close()

with open('ss.csv') as handle2:
    for w, tag in zip(writers, handle2):
        w[1].writerow(["  ", "Waybill", "Contents", "  ", "Amount", "COD Amount", "Type", "  ", "Error"])
        with open('1.csv', 'rb') as csvfile:
            handle1 = csv.reader(csvfile, delimiter=' ')
            for row in handle1:
                if tag.rstrip() in row:
                    w[1].writerow(row)

map(lambda w: w[0].close(), writers)