Importing and Exporting Data
PlexosDB provides methods for importing data from XML files and exporting to XML or CSV formats.
Importing from XML
Section titled “Importing from XML”Create a database from an existing PLEXOS XML file:
from plexosdb import PlexosDB
# Create database from XMLdb = PlexosDB.from_xml("/path/to/model.xml")
# Check PLEXOS version of the imported modelversion = db.versionprint(f"Imported PLEXOS model version: {version}")After importing, see Validating a Database to check model consistency and write findings to a text report.
Importing a PLEXOS solution ZIP and reading report tables
Section titled “Importing a PLEXOS solution ZIP and reading report tables”Use PlexosSolution when you want to analyze PLEXOS solution ZIP outputs. With
the default materialize="none", you can materialize only the table you need.
from plexosdb.solution_reader import PlexosSolutionimport pandas as pd
PLEXOS_SOLUTION = "/path/to/solution.zip"
sol = PlexosSolution.from_zip(PLEXOS_SOLUTION)sol.to_sqlite("output.sqlite", if_exists="replace")
table = "ST__Interval__Regions__Fixed_Load"sol.materialize_table(table, schema="report")df_table = pd.read_sql_query(f'SELECT * FROM report."{table}"', sol.connection)How to use this flow:
- Create a
PlexosSolutionviaPlexosSolution.from_zip(zip_path). - Call
to_sqlite(path, if_exists="replace")to import the ZIP into SQLite. Usematerialize="none"(default) to avoid materializing every derived table up-front. - Call
materialize_table(table, schema="report")for the specific table you want. - Read that table with pandas using
pd.read_sql_query(...)andsol.connection.
Exporting to XML
Section titled “Exporting to XML”Export your database to a PLEXOS-compatible XML file:
from pathlib import Path
# Export the entire database to XMLoutput_path = Path("/path/to/output_model.xml")success = db.to_xml(output_path)
if success: print(f"Model exported successfully to {output_path}")else: print("Export failed")CSV and database backups
Section titled “CSV and database backups”The import_from_csv(), to_csv(), and backup_database() methods are part of
the declared API but currently raise NotImplementedError. Use XML import and
export for supported file-based workflows.
Creating databases
Section titled “Creating databases”# Create an empty databasedb = PlexosDB()db.create_schema(version=10)