1

The script doesn't work as if I wanted.

Script:

mylayer = QgsProject.instance().mapLayersByName("Kuniarka")[0]

dane = 'a'
for seg in mylayer.getFeatures():
    #seg = next (mylayer.getFeatures())
    seg["WSP_X"] = dane
    mylayer.updateFeature(seg)

Effect: enter image description here

Desired effect: enter image description here

Script improvisation:

mylayer = QgsProject.instance().mapLayersByName("Kuniarka")[0]

dane = ["a", "b"]
for seg in mylayer.getFeatures():
    #seg = next (mylayer.getFeatures())
    seg["WSP_X"] = dane
    mylayer.updateFeature(seg)

Script improvisation2:

mylayer = QgsProject.instance().mapLayersByName("Kuniarka")[0]

for feat in mylayer.getFeatures():
    dane = ["a", "b"]
    mylayer.changeAttributeValue(feat.id(), 3, dane)

Script doesn't work correctly.

2

1 Answer 1

5

I think you're looking to access the list index.

dane = ['a', 'b']
print(dane[0]) #prints 'a'

In your code this would give (there are better ways in python to loop over 2 iterators):

from qgis.core import *
mylayer = QgsProject.instance().mapLayersByName("point1")[0]

dane = ['a', 'b', 'c', 'd', 'e']
i = 0
with edit(mylayer):
    for seg in mylayer.getFeatures():
        #seg = next (mylayer.getFeatures())
        print(seg.id())
        seg['name'] = dane[i]
        i += 1
        mylayer.updateFeature(seg)
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.