Python Safely Navigate a Complex Data Structure: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=
* [[Python_Language#Data_Structures|Python Language]]
*[[Python_Language_Dictionary#Safely_Navigate_a_Complex_Data_Structure|dict]]
*[[Python_Language_Dictionary#Safely_Navigate_a_Complex_Data_Structure|dict]]
* [[YAML_in_Python#Safely_Navigate_a_Complex_Data_Structure|YAML in Python]]
* [[YAML_in_Python#Safely_Navigate_a_Complex_Data_Structure|YAML in Python]]
* [[JSON processing in Python#Safely_Navigate_a_Complex_Data_Structure|JSON in Python]]
* [[JSON processing in Python#Safely_Navigate_a_Complex_Data_Structure|JSON in Python]]
=Overview=
=Overview=


Line 8: Line 10:
yaml = ...
yaml = ...
v = None
v = None
first_level_map = yaml.get('first_level_key', {}) # if 'first_level_key' does not exist at all, return {}, but if it exists and has nothing under it, it returns None.
first_level_map = yaml.get('first_level_key')  
if first_level_map:
if first_level_map:
   second_level_map = first_level_map.get('second_level_key', {})
   second_level_map = first_level_map.get('second_level_key')
   if second_level_map:
   if second_level_map:
     v = second_level_map.get('leaf_key')
     v = second_level_map.get('leaf_key')
return v
return v
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 04:50, 10 March 2022

Internal

Overview

yaml = ...
v = None
first_level_map = yaml.get('first_level_key') 
if first_level_map:
  second_level_map = first_level_map.get('second_level_key')
  if second_level_map:
    v = second_level_map.get('leaf_key')
return v