Python: Searching CSVs and exporting rows based on string macthes

Recently I had a need to search through a CSV log file, that was thousands of rows long and find certain names that existed in the log file and copy these rows to another CSV. I had to do this for dozens of names, each of which could have dozens of matches. Instead of searching for each of these names and copying the rows to another CSV file (which would have taken hours!) I decided to write a script that would do this task in a matter of seconds:

The script below opens one CSV; the file which you want to search. It then saves a new file with the results. There is a list at the start of of the program called people. You can add your own strings here which you are looking for in your CSV file:

 

The Script


import csv

people = [‘Joe Bloggs’, ‘Tim O Reilly’, ‘Mary Bloggs’]

with open(‘users.csv’) as oldfile, open(‘report.csv’, ‘w’) as newfile:
for line in oldfile:
if any(people in line for people in people):
newfile.write(line)

Example

Say we have a log file with 1000s of rows and we want to find all activity for the users Joe Bloggs, Tim O’Reilly and Mary Bloggs. The log file may look something like this:

User Action
Joe Bloggs Login
Mary Burke login
Darren O Neill Updated post
Joe Bloggs Updated post
Tim O Reilly login
Mary Bloggs login

Now if we run the script we can see the file returned only contains the users we are interested in.

Joe Bloggs Login
Joe Bloggs Updated post
Tim O Reilly login
Mary Bloggs login

Leave a Reply

Your email address will not be published. Required fields are marked *