简体   繁体   English

将单元格的输出保存为 jupyter notebook 中的 txt 文件

[英]Saving the output of a cell as a txt file in jupyter notebook

I would very much like to save the output of the last cell in a txt file.我非常想将最后一个单元格的输出保存在 txt 文件中。

 q = [rng.next () for _ in range (0, 25000000)]

I know I can use pandas dataframe but I need the txt file to carry out Diehard tests.我知道我可以使用 pandas 数据框,但我需要 txt 文件来执行 Diehard 测试。 Is it feasible in a jupyter notebook?在jupyter笔记本中可行吗? Possibly what type of data do I need to carry out the Dieharder Suite ?执行Dieharder Suite可能需要什么类型的数据?

I am almost sure that once I did something like that unfortunately I do not remember how, and I can not find a clear answer我几乎可以肯定,一旦我做了类似的事情,不幸的是我不记得怎么做了,而且我找不到明确的答案

Below is the code of my LCG random number generator:下面是我的 LCG 随机数生成器的代码:

import numpy as np

class LCG(object):

    UZERO: np.uint32 = np.uint32(0)
    UONE : np.uint32 = np.uint32(1)

    def __init__(self, seed: np.uint32, a: np.uint32, c: np.uint32) -> None:
        self._seed: np.uint32 = np.uint32(seed)
        self._a   : np.uint32 = np.uint32(a)
        self._c   : np.uint32 = np.uint32(c)

    def next(self) -> np.uint32:
        self._seed = self._a * self._seed + self._c
        return self._seed

    def seed(self) -> np.uint32:
        return self._seed

    def set_seed(self, seed: np.uint32) -> np.uint32:
        self._seed = seed

    def skip(self, ns: np.int32) -> None:
        """
        Signed argument - skip forward as well as backward

        The algorithm here to determine the parameters used to skip ahead is
        described in the paper F. Brown, "Random Number Generation with Arbitrary Stride,"
        Trans. Am. Nucl. Soc. (Nov. 1994). This algorithm is able to skip ahead in
        O(log2(N)) operations instead of O(N). It computes parameters
        A and C which can then be used to find x_N = A*x_0 + C mod 2^M.
        """

        nskip: np.uint32 = np.uint32(ns)

        a: np.uint32 = self._a
        c: np.uint32 = self._c

        a_next: np.uint32 = LCG.UONE
        c_next: np.uint32 = LCG.UZERO

        while nskip > LCG.UZERO:
            if (nskip & LCG.UONE) != LCG.UZERO:
                a_next = a_next * a
                c_next = c_next * a + c

            c = (a + LCG.UONE) * c
            a = a * a

            nskip = nskip >> LCG.UONE

        self._seed = a_next * self._seed + c_next


#%%
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, 25000000)]

Run below in another cell 在另一个单元格中下方运行

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

Then again another cell 然后又是另一个单元格

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

As a small note following ComplicatedPhenomenon 's answer, if we need capturing several functions, say fun1 , fun2 , fun3 and write the output to the corresponding files file1 , file2 , file3 , then we can capture with several variables:作为ComplicatedPhenomenon的回答之后的一个小注释,如果我们需要捕获多个函数,比如fun1fun2fun3并将输出写入相应的文件file1file2file3 ,那么我们可以使用几个变量进行捕获:

Cell 1单元格 1

%%capture cap_f1
fun1()

Cell 2单元格 2

%%capture cap_f2
fun1()

Cell 3单元格 3

%%capture cap_f3
fun1()

Summarize总结

(I prefer this ugly way because it allows fast line copying) (我更喜欢这种丑陋的方式,因为它可以快速复制行)

with open('file1', 'w') as f: f.write(cap_f1.stdout)
with open('file2', 'w') as f: f.write(cap_f2.stdout)
with open('file3', 'w') as f: f.write(cap_f3.stdout)

or或者

for fname, c in zip(listfilenames, listcaps):
    with open(fname, 'w') as f:
        f.write(c.stdout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM