在清除 csv 中的第二列数据时,我有以下代码(尝试):
def del_col(in_path):
# read file into memory
file_obj = open(in_path, 'rb')
reader = csv.reader(file_obj, delimiter='\t')
# delete the status column here:
# Now write csv back to same file:
# write data to file
file_obj = open(in_path, 'rb')
writer = csv.writer(file_obj)
writer.writerows(data)
file_obj.close()
我不确定是否或如何清除此列。我应该逐行阅读数据并清除每行的第二个条目吗?还是有一种更简单的方法可以一次简单地清除第二列?
这是第二次尝试:
def del_col(in_path):
# read file into memory
file_obj = open(in_path, 'rb')
reader = csv.reader(file_obj, delimiter='\t')
data = []
for row in reader:
# delete the status column here:
vals = [x[:1] + x[2:] for x in reader]
print vals
file_obj.close()
print 'Delete the 2nd Column (Lead Status?)'
conf = raw_input('delete these leads? (Y|N): ').upper()[0]
if conf == 'Y':
# write data to file
file_obj = open(in_path, 'wb')
writer = csv.writer(file_obj)
writer.writerows(data)
file_obj.close()