Save and export results

fileName asks the client to save the converted response before returning it.

Built-in behavior

Return representation File written
DataFrame <fileName>.csv
JSON string <fileName>.txt
Dictionary <fileName>.txt containing compact JSON
import pacifico

# Writes outputs/bond.csv and also returns the DataFrame.
frame = pacifico.request(
    "token.key",
    ticker="BCP0600323",
    format="dataFrame",
    fileName="outputs/bond",
)

# Writes outputs/bond_raw.txt and returns the JSON string.
raw = pacifico.request(
    "token.key",
    ticker="BCP0600323",
    format="json",
    fileName="outputs/bond_raw",
)

fileName is a base path

The extension is appended unconditionally:

fileName Format Actual path
"snapshot" DataFrame snapshot.csv
"snapshot.csv" DataFrame snapshot.csv.csv
"snapshot" JSON snapshot.txt
"snapshot.json" JSON snapshot.json.txt

Pass a string path without the final extension. A pathlib.Path does not support the string concatenation used internally; convert it with str(path).

Directory and overwrite behavior

  • Relative paths are resolved from the current working directory.
  • Parent directories are not created.
  • Existing files are overwritten without prompting.
  • Writes are not atomic.
  • DataFrame CSV output excludes the pandas index.
  • Dictionary output is serialized with json.dumps and no indentation.
  • An empty fileName disables saving.

Create directories first:

from pathlib import Path

output = Path("outputs")
output.mkdir(parents=True, exist_ok=True)

data = pacifico.request(
    "token.key",
    ticker="CHILE",
    fileName=str(output / "chile"),
)

Prefer explicit export for production

The built-in option is convenient for interactive work. Production workflows often need atomic replacement, encoding control, compression, schema metadata, or another format. Request the data and save it explicitly:

from pathlib import Path

frame = pacifico.request(
    "token.key",
    ticker="CHILE",
    format="dataFrame",
)

destination = Path("outputs/chile.csv")
temporary = destination.with_suffix(".csv.tmp")
destination.parent.mkdir(parents=True, exist_ok=True)

frame.to_csv(temporary, index=False)
temporary.replace(destination)

For raw JSON:

raw = pacifico.request(
    "token.key",
    ticker="CHILE",
    format="json",
)

Path("outputs/chile.json").write_text(raw, encoding="utf-8")

Data-handling checklist

Before persisting API output:

  1. confirm that the token's data license permits the destination and retention period;
  2. validate that the response is not an error row or error object;
  3. avoid shared directories with excessive permissions;
  4. encrypt sensitive datasets at rest when required;
  5. avoid embedding the token or temporary result URL in filenames or metadata;
  6. establish retention and deletion procedures for application uploads and local exports.