使用python清除或删除csv中的第二列
我有以下代码(尝试)用于清除csv中的第二列数据:使用python清除或删除csv中的第二列,python,csv,Python,Csv,我有以下代码(尝试)用于清除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:
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()
我只需阅读列表,而忽略第二列:
def del_col(in_path):
# read file into memory
file_obj = open(in_path, 'rb')
readfile = csv.reader(file_obj, delimiter='\t')
# delete the status column here:
vals = [row[:1] + row[2:] for row in readfile]
print vals
file_obj.close() # if you are going to use the same file name, close and re-open
# Now write csv to new file:
# write data to file
out_path = in_path + "_out.dat"
out_obj = open(out_path, 'w')
writefile = csv.writer(out_obj, delimiter='\t')
writefile.writerows(vals)
out_obj.close()
其他想法:不要写入与正在读取的文件相同的文件。(在本例中,我根据旧名称生成了一个新名称。)您需要使用
'w'
而不是'r'
打开目标文件,并将分隔符添加到csv.writer()
…中,我只需读取列表,而忽略第二列:
def del_col(in_path):
# read file into memory
file_obj = open(in_path, 'rb')
readfile = csv.reader(file_obj, delimiter='\t')
# delete the status column here:
vals = [row[:1] + row[2:] for row in readfile]
print vals
file_obj.close() # if you are going to use the same file name, close and re-open
# Now write csv to new file:
# write data to file
out_path = in_path + "_out.dat"
out_obj = open(out_path, 'w')
writefile = csv.writer(out_obj, delimiter='\t')
writefile.writerows(vals)
out_obj.close()
其他想法:不要写入与正在读取的文件相同的文件。(在本例中,我根据旧名称生成了一个新名称。)您需要使用
'w'
而不是'r'
打开目标文件,并将分隔符添加到csv.writer()
…您能看看我上面编辑的代码吗?我在其他函数中使用的布局中使用了中间代码部分,但无法运行。对此我深表歉意。您是否在编辑器中使用制表符或空格?其中可能混合了一些空白格式。请尝试再次复制(我已编辑),或删除生成错误的行周围的所有前导空白并重新插入,或在编辑器中显示不可见内容并确保其一致。Hmmm。。。我没有得到一个错误,但第二列仍然存在。如果我只想保留第1列,读卡器中x的VAL=[x[:0]是否有效?请确保您还更改了writer.writerows(VAL)
行。我将其更改为VAL
),以打印新变量,而不是原始代码中的变量。如果您只想保留第一列,您只需说VAL=[x[0]对于读卡器中的x]
(x
在本例中是从一行读取的每个值列表,因此您需要列表中的第一个)你能看一下我上面编辑过的代码吗?我使用了你的中间代码部分和我在其他函数中使用过的布局,但无法运行。对此感到抱歉。你是否在编辑器中使用制表符或空格?其中可能混合了一些空白格式。请尝试再次复制(我编辑过),或删除生成错误的行周围的所有前导空格并重新插入,或在编辑器中显示不可见项并确保其一致。嗯……我没有收到错误,但第二列仍然存在。VAL=[x[:0]如果我只想保留第一列,请确保您也更改了writer.writerows(vals)
行。我将其更改为vals
)以打印新变量,而不是原始代码中的变量。如果您只想保留第一列,您只需对reader中的x说vals=[x[0]
(x
在本例中是从一行读取的每个值列表,因此您需要列表中的第一个)使用下面的我的代码,而不是额外的for reader
循环中的行。vals=
行是for
循环的一行版本,它在reader中的每一行中运行,因此你的for row
行将把事情搞砸。我编辑了我的答案,以使用名称row
而不是x
,以避免混淆n、 …使用下面的代码,而不是读卡器循环中行的额外。vals=
行是
for循环的一行版本,它通过读卡器中的每一行运行,因此你的for row
行将把事情搞砸。我编辑我的答案是使用名称行
,而不是x
,以避免错误乌西。。。