glister11 glister11 - 7 months ago 11
Python Question

Read only rows with matching data in csv list with python

I'm new to python, and trying to understand how to sort data using the csv reader. I've read a couple of guides on this site, but nothing seems to quite relate to what I'm trying.

I have a csv file called buildings.csv, compiled with list data like this:


['VANGARDE MONKS CROSS', '1292', 'YO32 9AE', 'GREAT BRITAIN', 'Retail', 'Trading', '']
['VILLAGE DOUGLAS CORK SF', '418', 'CORK', 'REPUBLIC OF IRELAND', 'Retail', 'Trading', '']


What I want to know is, how do I return only rows in the list that contain the word 'IRELAND'? At the moment all I'm trying to do is get terminal to read the output from a text file.

Answer

Here is an easy way if you just want to print the lines:

with open('buildings.csv', 'r') as f:
    for line in f:
        if(line.find("IRELAND")!=-1):
            print(line)
Comments