继续尝试写入文件,但文件是空的:-(我尝试了很多事情,感觉自己在旋转。正如你所知,我是一个新手。任何帮助都将不胜感激!
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)
为什么要将csv.reader
传递给csv.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)
确保你了解这里发生了什么——如果不了解,请随时在评论中提问。