0

I have a query in MongoDB using Python:

for car in cars.find({"year": "2014"}):
    print car['V8']

The problem is, not every record has V8 entry exists. It will pop up error:

KeyError: 'V8'

No JSON object could be decoded

How can I ignore the error and just print empty string if the Key is not exist?

1 Answer 1

1

This looks like python, if so you can do

print car.get("V8", "")
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer, it will return None. And when I concatenate the result to some other string, it shows error "TypeError: coercing to Unicode: need string or buffer, NoneType found". Is there a workaround?
Edited. Try that, the second param to get() is what is returned when the key is not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.