2

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

3

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")
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.