I'm trying to iterate over each field in a shapefile. Then iterate over each record in that field.
First I build a list of all the fields, then iterate through that list. While I'm going through the list, I loop through all the features via feat = feature.GetField(x)
where x is the current field.
This works for the first field. It prints the first field, then all of its records. Then it proceeds to just loop through the rest of the fields, not the records. It's like it just runs the records for loop once. I have no idea why. I think it should work.
from osgeo import ogr
shapefile = r"E:\metadata_2\data\test.shp"
driver = ogr.GetDriverByName("ESRI Shapefile")
dataSource = driver.Open(shapefile, 0)
layer = dataSource.GetLayer()
layerDefinition = layer.GetLayerDefn()
layer_list = []
def getfields():
for i in range(layerDefinition.GetFieldCount()):
layer_name = layerDefinition.GetFieldDefn(i).GetName()
layer_list.append(layer_name)
print layer_list
for x in layer_list:
print 'This is the layer {}'.format(x)
for feature in layer:
feat = feature.GetField(x)
print feat
getfields()