这是我的代码到目前为止,我想要保留CSV中的许多行,但如果它是第3行,则忽略
如果不是第三行,这是我想省略的行:
Curriculum Name,,Organization Employee Number,Employee Department,Employee Name,Employee Email,Employee Status,Date Assigned,Completion Date,Completion Status,Manager Name,Manager Email
它每10行左右出现一次,但如果不是第一行(总是第三行),我希望将其删除
import csv, sys, os
#Read the CSV file and skipping the first 130 lines based on mylist
scanReport = open('Audit.csv', 'r')
scanReader = csv.reader(scanReport)
#search row's in csv - print out list
for file in glob.glob(r'C:\sans\Audit.csv'):
lineNumber = 0
str - "Curriculum Name"
with open('first.csv', 'rb') as inp, open('first_edit.csv', 'wb') as out:
writer = csv.writer(out)
for row in csv.writer(inp):
if row[2] != " 0":
writer.writerow(row)
答案 0 :(得分:0)
你想在那个循环中出现这样的事情:
index = 0
for row in csv.writer(inp):
if (index != 3) or (index == 3 and row[2] != " 0"):
writer.writerow(row)
index += 1
我对csv模块并不熟悉,所以我保留了你所有的东西,假设它是正确的(我不认为你需要那个模块来做你正在做的事情......)
有关enumerate
here的更多信息。
编辑: 要检查它是否是该行:
def IsThatLine(row):
return row[0] == "Curriculum Name" and row[1] == "" and row[2] == "Organization Employee" and ....
然后if
可以成为:
if (index != 3) or (index == 3 and not IsThatLine(row)):
答案 1 :(得分:-1)
请问您的问题更具体吗? 是否要删除包含以下描述的任何行?
课程名称,组织员工编号,员工部门,员工姓名,员工电子邮件,员工身份,分配日期,完成日期,完成状态,经理姓名,经理电子邮件
或者您是否只想删除此csv文件的第三行(行)?