.. Copyright (c) 2016, Johan Mabille, Sylvain Corlay and Wolf Vollprecht Distributed under the terms of the BSD 3-Clause License. The full license is in the file LICENSE, distributed with this software. File input and output ===================== *xtensor* has some built-in mechanisms to make loading and saving data easy. The base *xtensor* package allows to save and load data in the ``.csv``, ``.json`` and ``.npy`` format. Please note that many more input and output formats are available in the `xtensor-io `_ package. `xtensor-io` offers functions to load and store from image files (``jpg``, ``gif``, ``png``...), sound files (``wav``, ``ogg``...), HDF5 files (``h5``, ``hdf5``, ...), and compressed NumPy format (``npz``). Loading CSV data into xtensor ----------------------------- The following example code demonstrates how to use :cpp:func:`xt::load_csv` and :cpp:func:`xt::dump_csv` to load and save data in the Comma-separated value format. The reference documentation is :doc:`api/xcsv`. .. code:: #include #include #include #include #include int main() { std::ifstream in_file; in_file.open("in.csv"); auto data = xt::load_csv(in_file); std::ofstream out_file; out_file("out.csv"); xt::xarray a = {{1,2,3,4}, {5,6,7,8}}; xt::dump_csv(out_file, a); return 0; } Loading NPY data into xtensor ----------------------------- The following example demonstrates how to load and store xtensor data in the ``npy`` "NumPy" format, using the :cpp:func:`xt::load_npy` and :cpp:func:`xt::dump_npy` functions. Reference documentation for the functions used is found here :doc:`api/xnpy`. .. code:: #include #include #include #include #include int main() { // Note: you need to supply the data type you are loading // in this case "double". auto data = xt::load_npy("in.npy"); xt::xarray a = {{1,2,3,4}, {5,6,7,8}}; xt::dump_npy("out.npy", a); return 0; } Loading JSON data into xtensor ------------------------------ It's possible to load and dump data to json, using the json library written by `nlohmann` (https://nlohmann.github.io/json/) which offers a convenient way to handle json data in C++. Note that the library needs to be separately installed. The reference documentation is found :doc:`api/xjson`. .. code:: #include #include int main() { xt::xarray t = {{{1, 2}, {3, 4}}, {{1, 2}, {3, 4}}}; nlohmann::json jl = t; // To obtain the json serialized string std::string s = jl.dump(); xt::xarray res; auto j = "[[10.0,10.0],[10.0,10.0]]"_json; xt::from_json(j, res); }