2015-02-11 84 views
0

而不是使用output.csv如果我想有类别列表单词是文件名。我试过,但它不工作。如何让python读取csv文件并提取数据并粘贴另一个csv?

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

回答

0

如果我理解你的问题,首先你需要阅读这两个文件。第二个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) 
+0

不,它不是一门功课,其analysis.It任务是不工作的权利 – nik 2015-02-11 09:32:36

+0

现在是固定的。我试过 – gunzapper 2015-02-11 10:02:13

+0

谢谢,如果iI想要创建文件并将它们放入与尊敬的名称匹配的内容中,我将如何实现它? – nik 2015-02-11 10:06:49