KeyError but field exists (PyQGIS)












2















I have 10 fields in my shapefile, and I want to use Python (3.7) to access the value of one field in QGIS.



When I run my code (below) I get a KeyError: '6' (index position of the field), implying that the field lookup worked, but that once I try to access the value, the field doesn't exist.



f = layer.getFeature(fid)
index = layer.fields().lookupField('length')
print("length: ", f[index])


I also tried:



f = layer.getFeature(fid)
index = layer.fields().indexFromName('length')
print("length: ", f[index])


Both throw the same error.



Sample code uses just in a print statement, but I need to use the index number at various points/for different methods.










share|improve this question

























  • you are using indexand after is idx

    – Fran Raga
    4 hours ago











  • Try f.attributes()[idx] (qgis.org/api/… and gis.stackexchange.com/a/54059/2856)

    – user2856
    2 hours ago











  • @FranRaga corrected, just an error in my code transcribed here

    – jyingling
    14 mins ago
















2















I have 10 fields in my shapefile, and I want to use Python (3.7) to access the value of one field in QGIS.



When I run my code (below) I get a KeyError: '6' (index position of the field), implying that the field lookup worked, but that once I try to access the value, the field doesn't exist.



f = layer.getFeature(fid)
index = layer.fields().lookupField('length')
print("length: ", f[index])


I also tried:



f = layer.getFeature(fid)
index = layer.fields().indexFromName('length')
print("length: ", f[index])


Both throw the same error.



Sample code uses just in a print statement, but I need to use the index number at various points/for different methods.










share|improve this question

























  • you are using indexand after is idx

    – Fran Raga
    4 hours ago











  • Try f.attributes()[idx] (qgis.org/api/… and gis.stackexchange.com/a/54059/2856)

    – user2856
    2 hours ago











  • @FranRaga corrected, just an error in my code transcribed here

    – jyingling
    14 mins ago














2












2








2








I have 10 fields in my shapefile, and I want to use Python (3.7) to access the value of one field in QGIS.



When I run my code (below) I get a KeyError: '6' (index position of the field), implying that the field lookup worked, but that once I try to access the value, the field doesn't exist.



f = layer.getFeature(fid)
index = layer.fields().lookupField('length')
print("length: ", f[index])


I also tried:



f = layer.getFeature(fid)
index = layer.fields().indexFromName('length')
print("length: ", f[index])


Both throw the same error.



Sample code uses just in a print statement, but I need to use the index number at various points/for different methods.










share|improve this question
















I have 10 fields in my shapefile, and I want to use Python (3.7) to access the value of one field in QGIS.



When I run my code (below) I get a KeyError: '6' (index position of the field), implying that the field lookup worked, but that once I try to access the value, the field doesn't exist.



f = layer.getFeature(fid)
index = layer.fields().lookupField('length')
print("length: ", f[index])


I also tried:



f = layer.getFeature(fid)
index = layer.fields().indexFromName('length')
print("length: ", f[index])


Both throw the same error.



Sample code uses just in a print statement, but I need to use the index number at various points/for different methods.







pyqgis






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 14 mins ago







jyingling

















asked 5 hours ago









jyinglingjyingling

106110




106110













  • you are using indexand after is idx

    – Fran Raga
    4 hours ago











  • Try f.attributes()[idx] (qgis.org/api/… and gis.stackexchange.com/a/54059/2856)

    – user2856
    2 hours ago











  • @FranRaga corrected, just an error in my code transcribed here

    – jyingling
    14 mins ago



















  • you are using indexand after is idx

    – Fran Raga
    4 hours ago











  • Try f.attributes()[idx] (qgis.org/api/… and gis.stackexchange.com/a/54059/2856)

    – user2856
    2 hours ago











  • @FranRaga corrected, just an error in my code transcribed here

    – jyingling
    14 mins ago

















you are using indexand after is idx

– Fran Raga
4 hours ago





you are using indexand after is idx

– Fran Raga
4 hours ago













Try f.attributes()[idx] (qgis.org/api/… and gis.stackexchange.com/a/54059/2856)

– user2856
2 hours ago





Try f.attributes()[idx] (qgis.org/api/… and gis.stackexchange.com/a/54059/2856)

– user2856
2 hours ago













@FranRaga corrected, just an error in my code transcribed here

– jyingling
14 mins ago





@FranRaga corrected, just an error in my code transcribed here

– jyingling
14 mins ago










2 Answers
2






active

oldest

votes


















2














The solution is simpler:



print("length: ", f['length'])





share|improve this answer
























  • The goal of my code is not actually to print the value, I need to use other methods that require an index value and are also throwing the key error.

    – jyingling
    5 hours ago











  • @jyingling please update the question to reflect this requirement

    – underdark
    5 hours ago



















1














Use this



print("length: ", f.attributes()[index])


or loop all features



layer = iface.activeLayer()             
index = layer.fields().indexFromName('length')
for f in layer.getFeatures():
print (f.attributes()[index])





share|improve this answer


























  • Threw IndexError: list index out of range

    – jyingling
    7 mins ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "79"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f312420%2fkeyerror-but-field-exists-pyqgis%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The solution is simpler:



print("length: ", f['length'])





share|improve this answer
























  • The goal of my code is not actually to print the value, I need to use other methods that require an index value and are also throwing the key error.

    – jyingling
    5 hours ago











  • @jyingling please update the question to reflect this requirement

    – underdark
    5 hours ago
















2














The solution is simpler:



print("length: ", f['length'])





share|improve this answer
























  • The goal of my code is not actually to print the value, I need to use other methods that require an index value and are also throwing the key error.

    – jyingling
    5 hours ago











  • @jyingling please update the question to reflect this requirement

    – underdark
    5 hours ago














2












2








2







The solution is simpler:



print("length: ", f['length'])





share|improve this answer













The solution is simpler:



print("length: ", f['length'])






share|improve this answer












share|improve this answer



share|improve this answer










answered 5 hours ago









underdarkunderdark

68.1k13177341




68.1k13177341













  • The goal of my code is not actually to print the value, I need to use other methods that require an index value and are also throwing the key error.

    – jyingling
    5 hours ago











  • @jyingling please update the question to reflect this requirement

    – underdark
    5 hours ago



















  • The goal of my code is not actually to print the value, I need to use other methods that require an index value and are also throwing the key error.

    – jyingling
    5 hours ago











  • @jyingling please update the question to reflect this requirement

    – underdark
    5 hours ago

















The goal of my code is not actually to print the value, I need to use other methods that require an index value and are also throwing the key error.

– jyingling
5 hours ago





The goal of my code is not actually to print the value, I need to use other methods that require an index value and are also throwing the key error.

– jyingling
5 hours ago













@jyingling please update the question to reflect this requirement

– underdark
5 hours ago





@jyingling please update the question to reflect this requirement

– underdark
5 hours ago













1














Use this



print("length: ", f.attributes()[index])


or loop all features



layer = iface.activeLayer()             
index = layer.fields().indexFromName('length')
for f in layer.getFeatures():
print (f.attributes()[index])





share|improve this answer


























  • Threw IndexError: list index out of range

    – jyingling
    7 mins ago
















1














Use this



print("length: ", f.attributes()[index])


or loop all features



layer = iface.activeLayer()             
index = layer.fields().indexFromName('length')
for f in layer.getFeatures():
print (f.attributes()[index])





share|improve this answer


























  • Threw IndexError: list index out of range

    – jyingling
    7 mins ago














1












1








1







Use this



print("length: ", f.attributes()[index])


or loop all features



layer = iface.activeLayer()             
index = layer.fields().indexFromName('length')
for f in layer.getFeatures():
print (f.attributes()[index])





share|improve this answer















Use this



print("length: ", f.attributes()[index])


or loop all features



layer = iface.activeLayer()             
index = layer.fields().indexFromName('length')
for f in layer.getFeatures():
print (f.attributes()[index])






share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago

























answered 4 hours ago









Fran RagaFran Raga

2,990919




2,990919













  • Threw IndexError: list index out of range

    – jyingling
    7 mins ago



















  • Threw IndexError: list index out of range

    – jyingling
    7 mins ago

















Threw IndexError: list index out of range

– jyingling
7 mins ago





Threw IndexError: list index out of range

– jyingling
7 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to Geographic Information Systems Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f312420%2fkeyerror-but-field-exists-pyqgis%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Scriptures other than the Hata Yoga Pradipika containing asanas

De Pijp

90s novel: wood nymph (similar to an otik) who becomes a scientist