Skip to content Skip to sidebar Skip to footer

Using Python, How To Remove Whole Line In Csv File If Specific Character Is Present In This Line?

I have large csv file, 216961 lines: 9808,54,43,59,999,17,10,59,999,-1,0,0 9809,54,43,59,999,17,12,0,-1,0,0 9810,54,43,59,999,17,13,0,001,-1,0,0 9811,54,43,59,999,17,13,59,999,-1,0

Solution 1:

Try this approach.

import csv
reader = csv.reader(open("file.csv", "rb"), delimiter=',')
for line in reader:
    if"-1"notin line:
        print line

Solution 2:

If you have only one file, why not use vim?

:g/,-1,/d

If you have multiple files, you can try

  1. Search for a line with ,-1,
  2. Delete that line

Hints: look at fileinput

Post a Comment for "Using Python, How To Remove Whole Line In Csv File If Specific Character Is Present In This Line?"