2014-04-14 33 views
0

繼續嘗試寫入文件,但文件是空的:-(我已經嘗試了很多東西,感覺自己在旋轉,我是一個新手,你可以告訴任何幫助。不勝感激寫完後空文件

import shutil 
import re 
import csv 
import sys  # imports the sys module 
infile = csv.reader(open("March9small.csv", "rU"), dialect=csv.excel_tab) 
reader=csv.reader(infile) 
new_rows_list=[] 
for row in infile: 
    new_rows_list.append(row) 
    print new_rows_list 

outfile = csv.writer(open("March9small-revised.csv","wt"), dialect=csv.excel_tab) 
for i in new_rows_list: 
    outfile.writerow(i) 

回答

3

爲什麼你傳遞csv.readercsv.reader

這裏有沒有一箇中間表嵌套with上下文管理器簡化版本:!?

import csv 

with open("March9small.csv", "rU") as infile: 
    with open("March9small-revised.csv","wt") as outfile: 
     reader = csv.reader(infile, dialect=csv.excel_tab) 
     writer = csv.writer(outfile, dialect=csv.excel_tab) 

     for row in reader: 
      writer.writerow(row) 

確保你明白這裏發生了什麼 - 如果沒有,請隨時在評論中提問。