from osgeo import ogr
import re

gpkg = "years.gpkg"

ds = ogr.Open(gpkg, 1)  # 1 = write mode
layer = ds.GetLayer()

layer.ResetReading()

for feature in layer:
    location = feature.GetField("location")

    match = re.search(r"Y(\d{4})", location)
    if match:
        year = int(match.group(1))
        feature.SetField("year", year)
        layer.SetFeature(feature)

ds = None

print("Done: years populated")