In the Function editor I'm trying to edit a string something like:
from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def testfunc2(value1, value2, feature, parent):
s =trim(value1 + value2)
return s
I get the error:
Eval Error:
global name 'trim' is not defined
Can I use functions like trim, concat etc. in the Function editor?
mgri
16.4k6 gold badges48 silver badges80 bronze badges
1 Answer 1
Yes you can.
The problem is, that there is no method trim
available. But there is a method strip()
available on every string. You probably want to do
s = value1 + value2 # Concat
s = s.strip() # Trim
In general, you need to import functions. So if you did not import the function trim
, it will not be available.
answered Feb 16, 2016 at 14:56
-
Thanks, but how do I import functions like trim etc. I tried all the Qgis libraries like QGis.Utils.JAS– JAS2016年02月16日 15:09:39 +00:00Commented Feb 16, 2016 at 15:09
-
Why do you think that there should be a function
trim
in QGIS? Normally, if it's not geo-specific, read the python documentation or look for generic python modules and if you look for geo-specific stuff read the pyqgis cookbookMatthias Kuhn– Matthias Kuhn2016年02月16日 15:15:12 +00:00Commented Feb 16, 2016 at 15:15 -
I didn't but I hoped. In the QGis Expression dialog (2.12.3) you can use these functions for a regular expression. So I assumed they were also usable in the function editor which is also in the Expression Dialog.JAS– JAS2016年02月16日 15:52:19 +00:00Commented Feb 16, 2016 at 15:52
-
2Expression functions are not usable in the function editor. The function editor is pure python. But no reason to worry, python covers way more functionality than there are expression functions.Matthias Kuhn– Matthias Kuhn2016年02月16日 15:55:20 +00:00Commented Feb 16, 2016 at 15:55
-
1I found some good documented examples of python code in apps\python27\lib\string.py. Thank you for your time and quick response.JAS– JAS2016年02月16日 17:09:27 +00:00Commented Feb 16, 2016 at 17:09
lang-py