[英]Saving the output of a cell in jupyter notebook with a new line after each number
我將單元格的輸出保存為txt文件,如下所示:
第一個單元格:
%%capture cap --no-stderr
print(q)
第二單元格:
with open('output.txt', 'w') as f:
f.write(cap.stdout)
以下是我想保存的一小段代碼:
#%%
np.seterr(over='ignore')
a = np.uint32(1664525)
c = np.uint32(1013904223)
seed = np.uint32(1)
rng = LCG(seed, a, c)
q = [rng.next() for _ in range(0, 2500000)]
該文件已保存,但是生成的數字用逗號分隔,但是我希望每個生成的數字都用換行而不是逗號分隔
我嘗試將“ w”更改為“ a”並添加“ \\ n”,如下所示,但它對我不起作用。
with open('output.txt', 'a') as f:
f.write("\n")
%%capture
在此單元格中捕獲了其旁邊的所有代碼,因此您可以打印出列表中的所有元素。
%%capture cap --no-stderr
for i in q:
print(i)
with open('output.txt', 'w') as f:
f.write(cap.stdout)
cap.stdout
處理%%capture
整體捕獲的內容,因此,當您嘗試添加\\n
,它將無法正常工作。
是你想要的嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.