2

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
3
  • 1
    what about using logical operators like and (must satisfy all conditions) or or (satisfy any condition)? Commented Jan 20, 2019 at 11:14
  • no, it returns me an error saying list index out of range... Commented Jan 20, 2019 at 11:20
  • Hi, sorry about that, i had edited to show the error message i encountered. Commented Jan 20, 2019 at 11:26

2 Answers 2

2

You most probably have empty lines in your data - it may be simply a \n after the last line. 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.

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)
Sign up to request clarification or add additional context in comments.

1 Comment

You absolutely right! OMG, I was so stupid because I didn't check that there was actually an empty line in front, and i thought the code had some problems and spent my whole afternoon to fix it. Thank you very much for your super detail explanation and help!
2

You can "chain" multiple conditions using the logical operator and or or:

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

3 Comments

Hi thanks for the quickly, reply, but i dunno why it always give an error saying list index out of range..
In my example I'm accessing the rows values using the indices 1, 2 & 3. If some rows have no values for these indices, e.g. the row has only 2 values (length 2) you can't access row[2]. You can add an additional check if the row has all the values with something like if len(row) >= 2 and ... ignoring rows with missing values.
Your answer is right too, thank you for your help also!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.