I have a list with the field names and I am getting the values from the vector layer

shp = "D:\file.shp"
from osgeo import osr,ogr, gdal
driver = ogr.GetDriverByName('ESRI Shapefile')
dataSource = driver.Open(shp,0)

I am getting the follow error if I am doing it in the list

ValList = [feature.GetField(j) for j in list]

raise ValueError("Illegal field requested in GetField()")
ValueError: Illegal field requested in GetField()

So what I have to do if I want to get the values in the list?

  • 2
    Just a sidenote: don't call your variable list, that will hide a builtin type. – Matthias Kuhn Jul 12 '16 at 10:57

I think your error is found in the for-loop syntax. Try the following:

ValList = []
for j in lstFields:

Note that in your case, if you have already chosen a feature, change the last line to: ValList.append(Feature.GetField(j))

The results in my case are given below.

> lstFields = ['data1', 'data3'] # FieldsNames

> ValList
['B', 'AAA'] # These are the values I fed into these fields of the second feature.
  • Iam getting the same error. – user99 Jul 12 '16 at 11:39
  • Have you verified spelling for fields names? try at start only one field name, and see if it works. Later you can expand to include other fields. – dof1985 Jul 12 '16 at 11:40
  • In addition - note that python is case sensitive, e.g. in my example 'Data1' will fail, while 'data1' works fine. – dof1985 Jul 12 '16 at 12:06
  • Yes i am doing that correctly. – user99 Jul 12 '16 at 12:16
  • @user99 I guess that if you share a sample of your data, or make a reproducible example of this error, it might help. – dof1985 Jul 12 '16 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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