Retrieve specific rows of CSV file containing a particular keyword in Python
Question: I am currently trying to extract particular rows that contain certain
keyword(s)(e.g. 'battery' etc.) from a large csv file.

I have the following code written but it seems not to work for the filter
part.

    
    
        keywords={'battery'}
        import csv
        import sys
        csv.field_size_limit(sys.maxsize)
        invalids=0
        valids=0
        path=r'/Users/hung/Desktop/test.csv'
        with open (path,'r')as f:
            reader = csv.reader(f,delimiter=';')
            for row in reader:
                try:
                    print(row[2])
                    valids+=1
                except IndexError:
                    invalids+=1
                for field in row:
                    if field in keywords:
                        print(row)
                        break
        print(('parsed {0} records. ignored {1}').format(valids,invalids))
    

I am getting an error saying 'SyntaxError: invalid syntax' for 'print' in the
last line. Is there anything missing causing the error? Or is my code not
gonna work?

Thanks.


Answer: Replace:

    
    
    print(('parsed {0} records. ignored {1}').format(valids,invalids))
    

With

    
    
    print('parsed {0} records. ignored {1}'.format(valids,invalids))
    

See documentation for
[string.format](https://docs.python.org/3.5/library/string.html#format-string-
syntax)

