QGIS version 3.4
I need to copy all selected attributes from the field 'HomePostcode' into the field 'Postcode' through PyQGIS. Using the field calculator I would simply use the expression "HomePostcode", ensuring I'm updating field 'Postcode's selected features only.
I've searched high and low for relatable code and the following is the best I can come up with:
#adds layer to map
uri = 'C:/Users/xxxx/Cleaned.csv'
join_layer = iface.addVectorLayer(uri, 'Cleaned', 'ogr')
target_field = 'Postcode'
def calculate_attributes():
"""Calculates values for 'Postcode' by copying attributes from HomePostcode column and adds them to 'Postcode' column"""
with edit(join_layer):
for feature in join_layer.getFeatures():
feature.setAttribute(feature.fieldNameIndex('Postcode'), feature['HomePostcode'])
join_layer.updateFeature(feature)
print(f"Attribute calculated for {target_field} field")
calculate_attributes()
The Python console reads the script but I get a critical error "OGR error syncing to disk: Cannot rename files".
I know the above (if it worked) would just copy all values from HomePostcode to Postcode but I cannot find any clear documentation to target selected features only for QGIS 3.0.