Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Qgis Function grabbing a value from a list
from qgis.core import *
from qgis.gui import *
@qgsfunction(args='auto', group='Custom')
def values_from_list(string, number, feature, parent):
"""
Takes the n-th value from a list in a field where the list is
written like: (3:value1, value2,value3). Such lists are often a
result of importing gml. <br />
<h3>Syntax</h3>
values_from_list(<i>string</i>, <i>number</i>)
<h3>Arguments</h3><br/>
<i>string</i> The string containing the list in format like (3:value1, value2,value3) <br/>
<i>number</i> The index number of the value in the list to return
<h3>Examples</h3>
<pre>values_from_list("text", 1)</pre>
"""
components = string.strip('()').split(':',1)
values_list = components[1]
try:
return values_list.split(',')[number - 1]
except:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment