Skip to content
Advertisement

Export filters csv data to a new csv (python)

I would like to be able to read a csv file, filter out the data that I need based on the input of a user and then take that output and write it to a new CSV file.

A manual example would be to take a table in excel, filter specific columns to a certain value and take that filtered output and move it to a new file so you only have the filtered data.

I have a csv file that I am reading in

JavaScript

Advertisement

Answer

I believe part of the problem is that the values variable doesnt hold on to anything, as in it gets overwritten after every loop. Your print statement works because it is inside the loop but the return statement will only return the values for the last loop.

JavaScript

Since you wanted to export them to a csv file, instead make a list that will hold lists of the values:

JavaScript

Now you can export them to csv. Full code:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement