save_data¶
- pyhelpers.store.save_data(data, path_to_file, verbose=False, print_kwargs=None, show_warning=True, raise_error=False, **kwargs)[source]¶
Saves data to a file in a specific format.
- Parameters:
data (Any) –
The data to be saved, which can be:
a file in Pickle, CSV, Microsoft Excel, JSON, Joblib, Feather or Parquet format;
a URL of a web page or an HTML file;
an image file in a Matplotlib-supported format.
path_to_file (str | os.PathLike) – The path of the file where the
datawill be stored.verbose (bool | int) – Whether to print relevant information to the console; defaults to
False.print_kwargs (dict | None) – [Optional] Additional parameters passed to
pyhelpers.store._check_saving_path(). Defaults toNone.show_warning (bool) – Whether to display a warning message if an unknown error occurs; defaults to
True.raise_error (bool) – Whether to raise the provided exception; if
raise_error=False(default), the error will be suppressed.kwargs – [Optional] Additional parameters for one of the following functions:
save_pickle(),save_spreadsheet(),save_spreadsheets(),save_json(),save_joblib(),save_feather(),save_parquet(),save_geopackage(),save_figure()orsave_web_page_as_pdf().
Examples:
>>> from pyhelpers.store import save_data >>> from pyhelpers.dirs import cd >>> from pyhelpers._cache import example_dataframe >>> data_dir = cd("tests", "data") >>> # Get an example dataframe >>> data = example_dataframe() >>> data Longitude Latitude City London -0.127647 51.507322 Birmingham -1.902691 52.479699 Manchester -2.245115 53.479489 Leeds -1.543794 53.797418 >>> # Save the data to files different formats: >>> path_to_file = cd(data_dir, "dat.pickle") >>> save_data(data, path_to_file, verbose=True) Saving "dat.pickle" in "./tests/data/" ... Done. >>> path_to_file = cd(data_dir, "dat.csv") >>> save_data(data, path_to_file, verbose=True, index=True) Saving "dat.csv" in "./tests/data/" ... Done. >>> path_to_file = cd(data_dir, "dat.xlsx") >>> save_data(data, path_to_file, verbose=True, index=True) Saving "dat.xlsx" in "./tests/data/" ... Done. >>> path_to_file = cd(data_dir, "dat.txt") >>> save_data(data, path_to_file, verbose=True, index=True) Saving "dat.txt" in "./tests/data/" ... Done. >>> path_to_file = cd(data_dir, "dat.feather") >>> save_data(data, path_to_file, verbose=True, index=True) Saving "dat.feather" in "./tests/data/" ... Done. >>> path_to_file = cd(data_dir, "dat.parquet") >>> save_data(data, path_to_file, verbose=True) Saving "dat.parquet" in "./tests/data/" ... Done. >>> data = data.T.to_dict() # Convert `dat` to JSON format >>> path_to_file = cd(data_dir, "dat.json") >>> save_data(data, path_to_file, verbose=True, indent=4) Saving "dat.json" to "./tests/data/" ... Done.
See also
Examples for
load_data().