我在设计一个将从CSV文件中删除指定行和列的脚本时遇到问题。到目前为止,这是我的脚本:
if(session_status() == PHP_SESSION_ACTIVE)
{
session_regenerate_id();
}
这是我得到的追溯错误:
import csv
with open ("E:\\CSULB Grad\\2016 Spring\\Geog 588\\Project\\ACS_14_5YR_B02001_Copy.csv","rb") as csvfile:
reader=csv.reader(csvfile)
with open ("E:\\CSULB Grad\\2016 Spring\\Geog 588\\Project\\ACS_14_5YR_B02001_Copy.csv","ab+") as csvfile2:
writer=csv.writer(csvfile2)
for row in csvfile2:
del row['HD02_VD01']
writer.writerow(row)
答案 0 :(得分:1)
在迭代时,您无法就地修改CSV文件。请考虑创建一个新的CSV文件,该文件仅写入原始文件中每行所需的列。
例如:
with open(path,"wb") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames = fieldNames)
for row in csvfile2:
writer.writerow({"Col1":row['...'],"Col2":row['...']})