KeyError but field exists (PyQGIS)
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
add a comment |
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
you are usingindex
and after isidx
– Fran Raga
4 hours ago
Tryf.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
add a comment |
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
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
pyqgis
edited 14 mins ago
jyingling
asked 5 hours ago


jyinglingjyingling
106110
106110
you are usingindex
and after isidx
– Fran Raga
4 hours ago
Tryf.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
add a comment |
you are usingindex
and after isidx
– Fran Raga
4 hours ago
Tryf.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
index
and after is idx
– Fran Raga
4 hours ago
you are using
index
and 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
add a comment |
2 Answers
2
active
oldest
votes
The solution is simpler:
print("length: ", f['length'])
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
add a comment |
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])
Threw IndexError: list index out of range
– jyingling
7 mins ago
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
The solution is simpler:
print("length: ", f['length'])
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
add a comment |
The solution is simpler:
print("length: ", f['length'])
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
add a comment |
The solution is simpler:
print("length: ", f['length'])
The solution is simpler:
print("length: ", f['length'])
answered 5 hours ago
underdark♦underdark
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
add a comment |
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
add a comment |
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])
Threw IndexError: list index out of range
– jyingling
7 mins ago
add a comment |
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])
Threw IndexError: list index out of range
– jyingling
7 mins ago
add a comment |
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])
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])
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
you are using
index
and after isidx
– 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