2

I would like to concatenate the string of field1 with field2 separated by dot and so on. However, when I put the "." the attribute values remain null. How to separate string by dot?

e = QgsExpression('concat((lpad("field1",2,0)),".",(lpad("field2",2,0)),".",(lpad("field3",3,0)),".",(lpad("field4",4,0)))')
  • Could you add an example of a starting situation and the desired output? Then, it is unclear what you mean with the attribute values remain null. – mgri May 19 '17 at 21:42
  • @mgri for example: field1 = 1; field2 = 6; field3 = 15; field4 = 8. Desired output: 01.06.015.0008; When I execute the e function, no output is returned. However, if I execute this function in the field calculator the output will be generated as desired. – Jayme Muzzi May 22 '17 at 11:26
2

I did some tests (and some fails) on this code snippet:

layer=iface.activeLayer() # load the layer as you want
for feat in layer.getFeatures():
    e = QgsExpression('concat((lpad("field1",2,0)),".",(lpad("field2",2,0)),".",(lpad("field3",3,0)),".",(lpad("field4",4,0)))')
    value = e.evaluate()

before finding the solution, assuming that you are working on a layer with some fields ("field1", "field2", and so on).

Firstly, you can't call the fields in that way because they are not read correctly: writing "field1" as is doesn't provide its value but rather a string that stores the "field1" text. In fact, using the code snippet above with your expression would print:

print value
[f[.][f[.][fi[.][fie

which doesn't make any sense. So, the first step was slightly changing the sample code:

layer=iface.activeLayer() # load the layer as you want
for feat in layer.getFeatures():
    expr = 'concat((lpad(%s,2,0)),".",(lpad(%s,2,0)),".",(lpad(%s,3,0)),".",(lpad(%s,4,0)))' %(feat["field1"], feat["field2"], feat["field3"], feat["field4"])
    e = QgsExpression(expr)
    value = e.evaluate()

I thought it would be enough, but printing the value variable gave me:

print value
01[.]06[.]015[.]0008

I solved the issue by using the char() function instead of "." (or '.'). The char() function, available from the Field Calculator, returns the character associated with a Unicode code. For the (full stop) dot, the Unicode code is 46 (see this Wikipedia page for reference). This was the final code:

layer=iface.activeLayer() # load the layer as you want
for feat in layer.getFeatures():
    expr = 'concat((lpad(%s,2,0)),char(46),(lpad(%s,2,0)),char(46),(lpad(%s,3,0)),char(46),(lpad(%s,4,0)))' %(feat["field1"], feat["field2"], feat["field3"], feat["field4"])
    e = QgsExpression(expr)
    value = e.evaluate()

which printed the expected result:

print value
01.06.015.0008

Bonus If you want to store the result in a new field (e.g. "field5"), adding these lines to the last code should be enough for updating it:

layer=iface.activeLayer() # load the layer as you want
for feat in layer.getFeatures():
    expr = 'concat((lpad(%s,2,0)),char(46),(lpad(%s,2,0)),char(46),(lpad(%s,3,0)),char(46),(lpad(%s,4,0)))' %(feat["field1"], feat["field2"], feat["field3"], feat["field4"])
    e = QgsExpression(expr)
    value = e.evaluate()
    with edit(layer):
        feat["field5"] = value
        layer.updateFeature(feat)
1

Strings in expressions need to be wrapped in single quotation marks, i.e.

concat((lpad("field1",2,0)),'.',
       (lpad("field2",2,0)),'.',
       (lpad("field3",3,0)),'.',
       (lpad("field4",4,0)))
  • Did not work. Because the concat function must be wrapped in single quotation marks, otherwise concat is considered as variable. When I put concat wrapped by single quotation marks, gives syntax error in the dot ( '.') – Jayme Muzzi May 22 '17 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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