text: user4894: I searched the Python documentation for libSBML but couldn't find a staightforward answer to this question. For a given SBML model, how can I use Python libSBML to retrieve the values of the id and volume attributes for a given compartment?
  
  user4894: There aren't any examples in the python docs on how to do this. The best bet is to search the C/C++ docs which are more extensive. There is a python example in the docs which shows how to retrieve the number of compartments in a model but it doesn't go further. The code below shows how you can retrieve the information requested, assuming you have a model called testmodel.xml:
  import libsbml
  
  document = libsbml.readSBML("testmodel.xml")
  
  if document.getNumErrors() > 0:
     print("Encountered the following SBML errors:" + "\n")
     document.printErrors()
  else:       
     model = document.getModel()
  
     nCompartments = model.getNumCompartments()
  
     print ("Number of compartments: ", nCompartments)
     for i in range (nCompartments):
         comp = model.getCompartment(i)
  
         print ("Id = ", comp.getId())
         print ("Volume = ", comp.getVolume())
  
  
original_text: 4894: <p>I searched the Python documentation for libSBML but couldn't find a staightforward answer to this question. For a given SBML model, how can I use Python libSBML to retrieve the values of the <code>id</code> and <code>volume</code> attributes for a given compartment?</p>
   4894: <p>There aren't any examples in the python docs on how to do this. The best bet is to search the C/C++ docs which are more extensive. There is a python example in the docs which shows how to retrieve the number of compartments in a model but it doesn't go further. The code below shows how you can retrieve the information requested, assuming you have a model called testmodel.xml:</p>
  
  <pre><code>import libsbml
  
  document = libsbml.readSBML("testmodel.xml")
  
  if document.getNumErrors() &gt; 0:
     print("Encountered the following SBML errors:" + "\n")
     document.printErrors()
  else:       
     model = document.getModel()
  
     nCompartments = model.getNumCompartments()
  
     print ("Number of compartments: ", nCompartments)
     for i in range (nCompartments):
         comp = model.getCompartment(i)
  
         print ("Id = ", comp.getId())
         print ("Volume = ", comp.getVolume())
  </code></pre>
  