I want to replace a string in ALL FIELDS / Columns of table without having to do it using field calculator in every field manually using replace( "RoadName1" ,'Wharf','WharfXXX')
.
I have
layer = iface.activeLayer()
searchText = "Wharf"
replaceText = "WharfXXX"
for field in layer.fields():
fieldName=field.name()
print (fieldName)
for feature in layer.getFeatures():
inText=str(feature[fieldName])
#print (inText)
if searchText in inText:
#try QgsStringReplacement
QgsStringReplacement(inText,replaceText, False, False)
#try Field Calculator
print ("Replaced %s with %s" %(inText,replaceText))
else:
#print ("not found")
pass
It seems to work but I am not seeing any changes in the dataset.
I can't find any example implementations of https://qgis.org/api/classQgsStringReplacement.html
Does this function do what I think it's supposed to -i.e. replace text when it matches?
I guess I could configure field calc to use these inputs but the function above seems cleaner in the long run.
processing.run("qgis:fieldcalculator", {'INPUT':'test.tab|layername=test','FIELD_NAME':'RoadName','FIELD_TYPE':0,'FIELD_LENGTH':10,'FIELD_PRECISION':3,'NEW_FIELD':False,'FORMULA':' replace( \"RoadName\" ,\'Warner\',\'WarnerXXX\')','OUTPUT':'memory:'})