I am attempting to use the new Function Editor within QGIS 2.8.2. I have a field called LAND_NAME with hundreds of rows. What I'm trying to do is find and replace all "special characters". In ArcGIS, I would use this function:
codeblock:
def findreplace(s):
s = s.replace('(', '').replace(')','').replace('&','and').replace('\'','~').replace('.','').replace(' ','_').replace('@','at').replace(',','').replace('/','_').replace('[','').replace(']','').replace(':','_')
return s
In the function editor, I have gotten this far and am now stuck:
@qgsfunction(args='auto', group='Custom')
def findreplace(s):
s = replace("field",'(','')
return s
Any insights into how to replace all the special characters in the first codeblock and make a function in QGIS?
1 Answer 1
Like this:
@qgsfunction(args='auto', group='Custom')
def findreplace(s, feature, value):
chars = [('(', '')
,(')', '')
,('&', 'and')
,('\'', '~')
,('.', '')
,(' ', '_')
,('@', 'at')
,(', ', '')
,('/', '_')
,('[', '')
,(']', '')
,(':', '_')]
for char, replacewith in chars:
s = s.replace(char, replacewith)
return s
call it like this:
findreplace("LAND_NAME")