1

I have a python loop for in my cell in[16] that calls at every loop a function and that function print a bunch of strings. Since the number of iterations is big (the notebook file gets to 99+ MB), I would need the output those strings not inside the jupyter file but to a txt or csv file. I already read the answer mentioned here: how to save the output of a cell in iPython notebook?

However, if I use that code I get the following error message in pink:

ERROR:root:Line magic function %capture not found (But cell magic %%capture exists, did you mean that instead?).

and eventually, also this error message: NameError: name 'cap' is not defined

So just to give you an example. Let's say I have:

def test(i):
    print(i)


for i in range(1,10):
    test(i)

How do I export/print out the values from 1 to 10 inside a txt file instead of the notebook? I'm honestly not a python expert, so your help would be greatly appreciated.

Andrea
  • 113
  • 1
  • 4
  • 10

1 Answers1

5

enter image description here

Your function def print in first cell... Then in your second cell this:

%%capture cap --no-stderr
for i in range(1,5):
test(i)

In third cell this:

with open('output.txt', 'w') as f:
     f.write(cap.stdout)