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?

mgri
16.4k6 gold badges48 silver badges80 bronze badges
asked Jun 10, 2015 at 15:30

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")
answered Jun 10, 2015 at 22:46
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.