简体   繁体   English

如何基于多个条件删除csv文件中的行?

[英]How to delete a row in csv file based on multiple conditions?

I want to read a CSV file and delete a row in that CSV file based on three conditions, type , srcaddr , dstaddr . 我想读取CSV文件,并根据以下三个条件删除该CSV文件中的行: typesrcaddrdstaddr

So for example, if i enter something like first condition is 'icmp' , second condition is '10.0.0.1' , third condition is '10.0.0.2' , it will only delete that certain row. 因此,例如,如果我输入的第一个条件是'icmp'第二个条件是'10 .0.0.1'第三个条件是'10 .0.0.2' ,它将仅删除该特定行。

I have tried using this code but it only allows me to delete based on one condition only, how can I modify it to allow it to delete based on multiple conditions ? 我尝试使用此代码,但它仅允许我仅根据一个条件进行删除,如何修改它以允许其根据多个条件进行删除?

#code that I used to delete a row in CSV file based on only one condition.

import csv
import sys


with open('firewall-policies-test.csv', "rb") as f:
    data = list(csv.reader(f))

with open('firewall-policies-test.csv', "wb") as f:
    writer = csv.writer(f)
    for row in data:
        if row[0] != 'icmp':
              writer.writerow(row)   






 #code for CSV File
 type,srcaddr,dstadrr
 icmp,10.0.0.1,10.0.0.2
 tcp,10.0.0.1,10.0.0.2

It always give me an error saying list index out of range when i am trying to use and & or operators.. 当我尝试使用和&或运算符时,它总是给我一个错误,指出列表索引超出范围。

Traceback (most recent call last):
  File "deletecsv.py", line 11, in <module>
    if row[0] != "icmp" and row[1] != '10.0.0.1' and row[2] != '10.0.0.2':
IndexError: list index out of range

You can "chain" multiple conditions using the logical operator and or or : 您可以使用逻辑运算符andor “链接”多个条件:

if row[0] != 'icmp' and row[1] != 'foo' and row[2] != 'bar':
  # do thing

Here is are some more example and explanations: 这是更多示例和说明:

https://thomas-cokelaer.info/tutorials/python/boolean.html#boolean-examples https://thomas-cokelaer.info/tutorials/python/boolean.html#boolean-examples

You most probably have empty lines in your data - it may be simply a \\n after the last line. 您的数据中很可能有空行-它可能只是最后一行之后的\\ n。 It is parsed as well and does not contain any elements so row is a zero-length list - accessing row[1] then lets your program crash. 它也被解析并且不包含任何元素,因此row是一个零长度列表-访问row[1]会使程序崩溃。

Create demo file: 创建演示文件:

with open("firewall-policies-test.csv","w") as f:
     f.write("""type,srcaddr,dstadrr
 icmp,10.0.0.1,10.0.0.2
 tcp,10.0.0.1,10.0.0.2
 bla,10.0.0.3,10.0.0.4
""") # here is a line that does not hold 3 values - it holds nothing (== [])

Fix: 固定:

import csv
import sys 

# dont write / read text as binary    
with open('firewall-policies-test.csv', "r") as f:
    data = list(csv.reader(f))

with open('firewall-policies-test.csv', "w") as f:
    writer = csv.writer(f)
    for row in data:
        if row: # only use non-empty lines (or use if len(row)>2)
            if row[0] != 'icmp' and row[1] != '10.0.0.1' and row[2] != '10.0.0.2':
                  writer.writerow(row)

Test: 测试:

with open("firewall-policies-test.csv","r") as f:
    print(f.read())

Output: 输出:

type,srcaddr,dstadrr
 bla,10.0.0.3,10.0.0.4

The condition I use will remove any line that contains either of the conditions - to only delete rows that fullfill all your conditions use: 我使用的条件将删除包含任何一个条件的任何行-仅删除完全满足所有条件使用的行:

            if not (row[0] == 'icmp' and row[1] == '10.0.0.1' and row[2] == '10.0.0.2'): 
                writer.writerow(row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM