我有一个包含字段名称的列表,我正在尝试使用代码段获取字段值:
from osgeo import osr,ogr, gdal
shp="filepath"
driver = ogr.GetDriverByName('ESRI Shapefile')
dataSource = driver.Open(shp,0)
layer=dataSource.GetLayer()
list=['field1','field2','field3']
for i in layer:
ValList = [i.GetField(j) for j in list]
我收到以下错误:
raise ValueError("Illegal field requested in GetField()")
ValueError: Illegal field requested in GetField()
那么如果我想获取列表中的值,我该怎么做?
答案 0 :(得分:2)
list
变量名,因为它是一个保留的Python字。 这对我来说很好用:
from osgeo import ogr
shp= "your_shapefile_path"
driver = ogr.GetDriverByName('ESRI Shapefile')
dataSource = driver.Open(shp, 0)
layer = dataSource.GetLayer()
list_field = ['existing_field']
for feature in layer:
values_list = [feature.GetField(j) for j in list_field]