# How to write the output from previous cell to a csv file?

**URL:** <https://discourse.jupyter.org/t/how-to-write-the-output-from-previous-cell-to-a-csv-file/10319>\
**Category:** JupyterHub\
**Created:** [August 10, 2021, 3:56am UTC](https://discourse.jupyter.org/t/how-to-write-the-output-from-previous-cell-to-a-csv-file/10319 "2021-08-10T03:56:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahantesh19](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/mahantesh19/32/4960_2.png) [@mahantesh19](https://discourse.jupyter.org/u/mahantesh19)\
**Post date:** [August 10, 2021, 3:56am UTC](https://discourse.jupyter.org/t/how-to-write-the-output-from-previous-cell-to-a-csv-file/10319/1 "2021-08-10T03:56:51Z")

</div>

I would like to know how I can write the output from the previous cell to a csv file?

 ![Screenshot 2021-08-10 113801](https://canada1.discourse-cdn.com/flex031/uploads/jupyter/original/2X/7/734a23a19b453263883f35f4eb081361a519cc10.jpeg)

---

<div class="post-metadata">

**Author:** ![fomightez](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/fomightez/32/495_2.png) [@fomightez](https://discourse.jupyter.org/u/fomightez)\
**Post date:** [August 10, 2021, 4:05pm UTC](https://discourse.jupyter.org/t/how-to-write-the-output-from-previous-cell-to-a-csv-file/10319/2 "2021-08-10T16:05:07Z")

</div>

IPython/Jupyter magics can help you here. Specifically, the ones I suggest using are documented [here](https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-capture) and [here](https://ipython.readthedocs.io/en/stable/config/extensions/storemagic.html). (Succinct coverage of the use of `%store` [here](https://stackoverflow.com/a/32731418/8508004).)

Add above first line of the previous cell the following cell magic as a line:

```python
%capture out

```

Then run your previous cell.

Now in the cell below that to save the captured output from that previous cell:

```python
# write the captured stdout channel to a file
%store out.stdout >my_data.csv

```

* * *

Optionally, if you want what would have been the output from the cell where you added the `%%capture` magic to appear in the notebook as well, add a cell above or below where you used `%store`, with the following contents:

```python
# show here the content from captured stdout channel 
sys.stdout.write(out.stdout)

```

`print(out.stdout)` seems to work as well.  
The basis for `out.stdout` being the way to invoke is described [here](https://ipython.readthedocs.io/en/stable/api/generated/IPython.utils.capture.html#IPython.utils.capture.CapturedIO).

Alternatively, you could use `%load my_data.csv` after the `%store` command to show the contents of the saved file in the notebook.

* * *

The capture approach can be used for rich output as well, see [use with plots here](https://discourse.jupyter.org/t/how-to-display-plots-only-after-plt-show-in-jup-notebook/2863/2).

* * *

For the sake of completeness, I’ll add there is also the following method for `IPython.utils.capture.CapturedIO`:

```python
out.show()

```

(`out.show()` can be replaced with `out()`)  
The documentation [here](https://ipython.readthedocs.io/en/stable/api/generated/IPython.utils.capture.html#IPython.utils.capture.CapturedIO) makes it seem like that would be a reasonable way to also display the captured results in the notebook; however, for some reason, at present showing `out` those ways in the notebook also appends an additional line that is the count of the number of characters in the output string shown above. (This seems less than ideal to me. However, maybe I am missing something why someone using the `show()` method would want that or if it is the result of some other notebook property adding that line. It doesn’t seem to happen with rich outputs, see [here](https://discourse.jupyter.org/t/how-to-display-plots-only-after-plt-show-in-jup-notebook/2863/2), and maybe it is because this is a stdout channel object in this example.)

---

<div class="post-metadata">

**Author:** ![mahantesh19](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.jupyter.org/mahantesh19/32/4960_2.png) [@mahantesh19](https://discourse.jupyter.org/u/mahantesh19)\
**Post date:** [August 11, 2021, 3:20am UTC](https://discourse.jupyter.org/t/how-to-write-the-output-from-previous-cell-to-a-csv-file/10319/3 "2021-08-11T03:20:20Z")

</div>

Thank you very much for your explanation.
