4

I currently have 2 columns NOM_CONCAT and Number of concat(COUNT_RSF_)

which is composed of" concat "from 0 to 5

A1, B1, C1 ...

A2

A3, B3

.
.

My goal and to separate the column NOM_CONCAT in several columns under PyQGIS except that this does not mean this to separate.

Without hiding, the end goal is to rotate the generated columns.

from PyQt5.QtCore import *
from qgis.core import *
from qgis.PyQt.QtCore import QVariant
uri="C:\impression_plan\mini\RSFFicheCanalisation.shp"

v1= QgsVectorLayer(uri, "RPG_grille_2018", "ogr")
all_features = v1.getFeatures()
v1.startEditing()
pr = v1.dataProvider()
#l=1
idx = v1.fields().lookupField('COUNT_RSF_')
idmax =  v1.maximumValue(idx)

print(idmax)
v1 = iface.activeLayer()
fieldindex = v1.fields().indexFromName("NOM_CONCAT") 

v1.startEditing() 
for feat in v1.getFeatures(): 

    if feat[fieldindex]: 
        fields = feat[fieldindex].split(', ')
        print(fields)

        for i in range(1, len(fields)): 

            feat[fieldindex + i] = fields[i - 1] 
            v1.updateFeature(feat) 
    else: 
        continue 
v1.commitChanges()

QgsProject.instance().addMapLayer(v1)
v1.updateFields

enter image description here

5
  • Give us an example of input columns and output columns in your attribute table. Use Excel or a spreadsheet if that helps.
    – wingnut
    May 14, 2021 at 13:38
  • thank you for your answers I added a photo to the question May 14, 2021 at 13:57
  • So you only want the FIRST item from NOM_CONCAT?
    – wingnut
    May 14, 2021 at 14:28
  • @wingnut looks like the goal is to have a separate row for each item in NOM_CONCAT May 14, 2021 at 14:37
  • I see. The OP'S code is modifying existing features rather than creating new ones. @patrickblancseau - you need to create new features for every A,B,C class in a NEW layer. Don't modify the existing layer features. It will be neater.
    – wingnut
    May 14, 2021 at 14:43

1 Answer 1

11

You can use "Split features by character" tool.

uri = r"C:\impression_plan\mini\RSFFicheCanalisation.shp"

layer = QgsVectorLayer(uri, "", "ogr")

new_layer = processing.runAndLoadResults(
                "native:splitfeaturesbycharacter",
                {'INPUT': layer, 
                 'FIELD':'NOM_CONCAT',
                 'CHAR':', ',
                 'REGEX': False,
                 'OUTPUT':'TEMPORARY_OUTPUT'})["OUTPUT"]

Before:

enter image description here

After:

enter image description here

0

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.