I'm following the book 'Geoprocessing with python', and I'm just trying to filter countries by their continent with this code:

from osgeo import ogr
import ospybook as pb
fn = os.path.join(data_dir, 'ne_10m_admin_0_countries')
ds = ogr.Open(fn)
lyr = ds.GetLayer('ne_10m_admin_0_countries')
lyr.SetAttributeFilter('CONTINENT = "Asia"')

I can open and read the file with all the attributes, but when I try to use the SetAttributeFilter with Asia for example , I always get the answer:

ERROR 1: "Asia" not recognised as an available field.

I've tried this with diffrent shapefiles and always get the same error.

1 Answer 1


I simply swapped the single/double quotes in

lyr.SetAttributeFilter('CONTINENT = "Asia"')

So, the correct syntax is

lyr.SetAttributeFilter("CONTINENT = 'Asia' ")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.