save_data
- pyhelpers.store.save_data(data, path_to_file, err_warning=True, confirmation_required=True, **kwargs)
Save data to a file of a specific format.
- Parameters:
data (any) – data that could be saved to a file of Pickle, CSV, Microsoft Excel, JSON, Joblib or Feather format; a URL of a web page or an HTML file; or an image file of a Matplotlib-supported format
path_to_file (str or os.PathLike[str]) – pathname of a file that stores the
data
err_warning (bool) – whether to show a warning message if any unknown error occurs, defaults to
True
confirmation_required (bool) – whether to require users to confirm and proceed, defaults to
True
kwargs – [optional] parameters of one of the following functions:
save_pickle()
,save_spreadsheet()
,save_json()
,save_joblib()
,save_feather()
,save_fig()
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 >>> dat = example_dataframe() >>> dat 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: >>> dat_pathname = cd(data_dir, "dat.pickle") >>> save_data(dat, dat_pathname, verbose=True) Saving "dat.pickle" to "tests\data\" ... Done. >>> dat_pathname = cd(data_dir, "dat.csv") >>> save_data(dat, dat_pathname, index=True, verbose=True) Saving "dat.csv" to "tests\data\" ... Done. >>> dat_pathname = cd(data_dir, "dat.xlsx") >>> save_data(dat, dat_pathname, index=True, verbose=True) Saving "dat.xlsx" to "tests\data\" ... Done. >>> dat_pathname = cd(data_dir, "dat.txt") >>> save_data(dat, dat_pathname, index=True, verbose=True) Saving "dat.txt" to "tests\data\" ... Done. >>> dat_pathname = cd(data_dir, "dat.feather") >>> save_data(dat, dat_pathname, index=True, verbose=True) Saving "dat.feather" to "tests\data\" ... Done. >>> # Convert `dat` to JSON format >>> import json >>> dat_ = json.loads(dat.to_json(orient='index')) >>> dat_ {'London': {'Longitude': -0.1276474, 'Latitude': 51.5073219}, 'Birmingham': {'Longitude': -1.9026911, 'Latitude': 52.4796992}, 'Manchester': {'Longitude': -2.2451148, 'Latitude': 53.4794892}, 'Leeds': {'Longitude': -1.5437941, 'Latitude': 53.7974185}} >>> dat_pathname = cd(data_dir, "dat.json") >>> save_data(dat_, dat_pathname, indent=4, verbose=True) Saving "dat.json" to "tests\data\" ... Done.
See also
Examples for the function
pyhelpers.store.load_data()
.