从cs中删除多行

2024-06-28 10:55:19 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的代码到目前为止,我有许多行在一个CSV,我想保留,但如果它是第三行,然后忽略

如果不是第三行,我想省略这一行:

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)

Tags: csvnameindateemailstatusemployeeopen
2条回答

你的问题能说得更具体些吗? 是否要删除包含以下描述的任何行?你知道吗

课程名称、组织员工编号、员工部门、员工姓名、员工电子邮件、员工状态、分配日期、完成日期、完成状态、经理姓名、经理电子邮件

还是只删除此csv文件的第三行(行)?你知道吗

你想在那个循环中得到这样的东西:

index = 0
for row in csv.writer(inp):
    if (index != 3) or (index == 3 and row[2] != " 0"):
        writer.writerow(row)
    index += 1

我不熟悉csv模块,所以我保留了你所有的东西,假设它是正确的(我不认为你需要这个模块来做你正在做的事情…)

有关enumeratehere的详细信息。你知道吗

编辑: 要检查是否是那条线:

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)):

相关问题 更多 >