How do I get the compartment Id and volume from SBML using libSBML Python? 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?
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() &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())