I want to read a CSV file and delete a row in that CSV file based on three conditions, type, srcaddr, dstaddr.
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.
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
and(must satisfy all conditions) oror(satisfy any condition)?