Reading from Python dict if key might not be present

后端 未结 7 491
清酒与你
清酒与你 2021-01-30 17:10

I am very new to Python and parsing data.

I can pull an external JSON feed into a Python dictionary and iterate over the dictionary.

for r in results:
         


        
7条回答
  •  暖寄归人
    2021-01-30 17:35

    You can use the built in function hasattr

    key='key_name'
    # or loop your keys 
    
    if hasattr(e, key):
      print(e[key])
    else:
      print('No key for %s' % key)
    

提交回复
热议问题