如何在python中删除行CSV [英] How to Delete Rows CSV in python
问题描述
我试图比较两个csv文件(fileA和fileB),并删除fileA中找不到fileB中的任何行。我想能够做到这一点,而不创建第三个文件。我想我可以使用csv writer模块,但现在我第二个猜测自己。
目前,我使用下面的代码来记录我的比较数据文件B:
remove_list = set()
with open('fileB','rb')as file_b:
reader1 = csv.reader(file_b)
next(reader1)
for reader1:
remove_list.add((row [0],row [2]))
这是我被卡住的地方,不知道如何删除这些行:
与open('fileA','ab')as file_a:
with open('fileB','rb')as file_b :
writer = csv.writer(file_a)
reader2 = csv.reader(file_b)
next(reader2)
对于reader2中的行:
if(row [ 0],row [2])不在remove_list中:
#如果行不存在于文件B中,则从文件A中删除它。
#stuck here:writer。< HowDoIRemoveRow>此解决方案使用 docs.python.org/2/library/fileinput.html> fileinput
inplace = True
,它写入一个临时文件,然后自动将其重命名到文件名的末尾。您不能从文件中删除行,但只能使用您想要的行重写。
如果将 inplace = 1
的关键字参数传递给 fileinput.input()
或 FileInput
构造函数,文件被移动到备份文件,标准输出被定向到输入文件(如果与备份文件同名的文件已经存在,它将被静默替换)。
fileA
h1,h2,h3
a,b,c
d,e,f
g,h,i
j,k, l
fileB
h1,h2,h3
a,b,c
1,2,3
g,h,i
4,5,6
import fileinput,sys,csv
with open('fileB','rb')as file_b:
r = csv.reader(file_b)
next(r)#skip header
seen = { (row [0],row [2])for row in r}
f = fileinput.input('fileA',inplace = True)#sys.stdout重定向到文件
打印next(f),#写头作为第一行
w = csv.writer(sys.stdout)
对于csv.reader(f)中的行:
if [0],row [2])in:see如果它在B中则写入
w.writerow(row)
fileA
h1,h2,h3
a,b,c
g,h,i
I'm trying to compare two csv files (fileA and fileB), and remove any rows from fileA that are not found in fileB. I want to be able to do this without creating a third file. I thought I could do this using the csv writer module but now I'm second guessing myself.
Currently, I'm using the following code to record my comparison data from file B:
removal_list = set()
with open('fileB', 'rb') as file_b:
reader1 = csv.reader(file_b)
next(reader1)
for row in reader1:
removal_list.add((row[0], row[2]))
This is where I'm stuck and do not know how to delete the rows:
with open('fileA', 'ab') as file_a:
with open('fileB', 'rb') as file_b:
writer = csv.writer(file_a)
reader2 = csv.reader(file_b)
next(reader2)
for row in reader2:
if (row[0], row[2]) not in removal_list:
# If row was not present in file B, Delete it from file A.
#stuck here: writer.<HowDoIRemoveRow>(row)
解决方案 This solution uses fileinput
with inplace=True
, which writes to a temporary file and then automatically renames it at the end to your file name. You can't remove rows from a file but you can rewrite it with only the ones you want.
if the keyword argument inplace=1
is passed to fileinput.input()
or to the FileInput
constructor, the file is moved to a backup file and standard output is directed to the input file (if a file of the same name as the backup file already exists, it will be replaced silently). This makes it possible to write a filter that rewrites its input file in place.
fileA
h1,h2,h3
a,b,c
d,e,f
g,h,i
j,k,l
fileB
h1,h2,h3
a,b,c
1,2,3
g,h,i
4,5,6
import fileinput, sys, csv
with open('fileB', 'rb') as file_b:
r = csv.reader(file_b)
next(r) #skip header
seen = {(row[0], row[2]) for row in r}
f = fileinput.input('fileA', inplace=True) # sys.stdout is redirected to the file
print next(f), # write header as first line
w = csv.writer(sys.stdout)
for row in csv.reader(f):
if (row[0], row[2]) in seen: # write it if it's in B
w.writerow(row)
fileA
h1,h2,h3
a,b,c
g,h,i
这篇关于如何在python中删除行CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!