Saving the output of a cell in jupyter notebook with a new line after each number

前端 未结 1 1208
忘了有多久
忘了有多久 2021-01-25 16:36

I saved output of a cell as a txt file as follows:

First cell:

%%capture cap --no-stderr
print(q)

Second cell:

with ope         


        
1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-25 17:06

    %%capture capture all the out of the code beside it in this cell, so you can print out all the elements from the list.

    %%capture cap --no-stderr
    for i in q:
        print(i)
    

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

    cap.stdout handle what %%capture captured as a whole, so when you tried to add \n, it won't work.

    Is it what you want?

    0 讨论(0)
提交回复
热议问题