0

I am trying to separate a string value from a field and then put it into its releated Field.

The field name are characters in the string and its value is the number.

"Field"=PT 100BC 20cd 19 is the string value

PT= 100, BC=20 cd=19.

the string is irregular.

the column names are the characters in the string and number is the value associated with it.

I have so far done this.

from qgis.core import *
import qgis.utils

inlayer=iface.activeLayer()

inlayer.selectByExpression('"TRTMTHD1"=\'PLANT\'or "TRTMTHD1"=\'NATURAL\' ')
selection=inlayer.selectedFeatures()

for s in selection:
    d=(sl.split() for sl in s)

If they do the split the result is PT, 100BC, 20CD which I do not want.

Which command do I use to split the value in field column and then update the record?

After I run this

{from qgis.core import * import qgis.utils

layer=iface.activeLayer() fc=layer.featureCount() f_list=[] for i in range(0,fc): feat=layer.getFeature(i) f_list.append(feat['SPCOMP'])

field_list=[] for f in layer.fields(): if len(f.name())==2: field_list.append(f.name())

layer.selectByExpression('"TRTMTHD1"=\'PLANT\'or "TRTMTHD1"=\'NATURAL\' ') selection=layer.selectedFeatures()

for string in f_list: if string!=None: for fn in field_list: string=string.replace(fn,',' + fn)

list_of_field_values=(string.split(',') for string in f_list)
for fvl in list_of_field_values:
    if fvl !='':
        fd,vl=fvl.split(' ')
        print('field=',fd,'value=',vd)}



I get AttributeError: 'list' object has no attribute 'split'
0

1 Answer 1

-1
# splitting the string
list_of_fieldnames = ['PT', 'BC', 'cd', 'f1', 'f2', 'f3']

string1 = 'PT 100BC 20cd 19'
string2 = 'f3 3f1 1PT 100cd 19'

for string in [string1, string2]:
    for field in list_of_fieldnames:
        string = string.replace(field, ',' + field)

    list_of_fields_and_values = string.split(',')
    print(list_of_fields_and_values)
    for field_and_value in list_of_fields_and_values:
        if field_and_value != '':
            field, value = field_and_value.split(' ')
            print('field=', field, 'value=', value)

# update field value
# the layer is qgis.core.QgsVectorLayer
# https://qgis.org/pyqgis/3.6/core/QgsVectorLayer.html

layer.startEditing()
# check fid, field, newValue parameters at https://qgis.org/pyqgis/3.6/core/QgsVectorLayer.html#qgis.core.QgsVectorLayer.changeAttributeValue
layer.changeAttributeValue(fid, field, newValue)
layer.commitChanges()
6
  • Thanks for the solution. but i cannot hard code the value. the string changes through out the column. it is a irregular string and some value start with different characters like 'pj'.
    – JaySharma
    Commented Sep 23, 2019 at 4:46
  • the problem is in your string - there is no separators between fieldnames, but it is still possible split the with a little tricks Commented Sep 23, 2019 at 5:03
  • if you could please help me on this. I am at beginner level to Qgis and python.
    – JaySharma
    Commented Sep 23, 2019 at 5:06
  • @JaySharma see update Commented Sep 23, 2019 at 5:30
  • please look at update i made.
    – JaySharma
    Commented Sep 23, 2019 at 9:15

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.