I have a list (named Responses
) which contains some dictionary with the following key-value pair:
Responses = [
{'Id':100,'ResponseText1':'aaaa','ResponseText2':'yyyy'},
{'Id':101,'ResponseText1':'bbbb','ResponseText2': 'zzzz'},
{'Id':103,'ResponseText1':'cccc'},
{'Id':104,'ResponseText2': True},
]
As you can see here Id
is a common field for all dictionary
, but ResponseText1
and ResponseText2
are unavailable for some dictionary
.
I want to separate and detect all the data to push them into my database. My python
code:
for j in Responses:
print(j['Id'])
if j['ResponseText1']:
print(j['ResponseText1'])
else:
print("No ResponseText1 is present")
if j['ResponseText2']:
print(j['ResponseText2'])
else:
print("No ResponseText2 is present")
But It says errors like:
Traceback (most recent call last):
File "<string>", line 14, in <module>
KeyError: 'ResponseText2'
Please suggest how can I fix this?