If you have already set the column names of parcel_num
and parcel_den
in the attribute table, you can run the following code which will extract values before and after the forward slash /
into separate columns. The code assumes the column types of the parcel_num
and parcel_den
are integers.
layer = iface.activeLayer()
with edit(layer):
for feature in layer.getFeatures():
value = feature['parcel_val'] # update the field name of the parcel_value
print(value)
feature['parcel_num'] = value.split('/')[0]
feature['parcel_den'] = value.split('/')[1]
layer.updateFeature(feature)
Before running the code:
After running the code:
Without PyQGIS, you can also achieve the same results using Field Calculator:
Check Update Existing Field
and select parcel_num
Write the following expression
regexp_replace("parcel_val" ,'(\\d+)/(\\d+)','\\1')
Do the same steps for parcel_den
Check Update Existing Field
and select parcel_den
Write the following expression
regexp_replace("parcel_val" ,'(\\d+)/(\\d+)','\\2')
Where parcel_val
is the name of the field that holds the parcel values, for example, 247/34
.
You will get the same results as before.