使用python删除csv文件中的特定行 [英] Remove a specific row in a csv file with python
本文介绍了使用python删除csv文件中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果第二列与字符串匹配,我正在尝试从csv文件中删除一行.我的csv文件包含以下信息:
I am trying to remove a row from a csv file if the 2nd column matches a string. My csv file has the following information:
Name
15 Dog
我希望删除其中带有名称"的行.我正在使用的代码是:
I want the row with "Name" in it removed. The code I am using is:
import csv
reader = csv.reader(open("info.csv", "rb"), delimiter=',')
f = csv.writer(open("final.csv", "wb"))
for line in reader:
if "Name" not in line:
f.writerow(line)
print line
但是名称"行并未删除.我在做什么错了?
But the "Name" row isn't removed. What am I doing wrong?
我使用了错误的定界符.将其更改为\ t可行.下面是现在可以使用的代码. 导入csv
I was using the wrong delimiter. Changing it to \t worked. Below is the code that works now. import csv
reader = csv.reader(open("info.csv", "rb"), delimiter='\t')
f = csv.writer(open("final.csv", "wb"))
for line in reader:
if "Name" not in line:
f.writerow(line)
print line
推荐答案
似乎您在csv.reader中指定了错误的定界符(逗号)
Seems that you are specifying the wrong delimiter (comma)in csv.reader
这篇关于使用python删除csv文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文