如何允许 c printf 在 cython 单元中的 ipython 笔记本中打印?

col*_*ang 6 c cython jupyter-notebook

%%cython
from libc.stdio cimport printf
def test():
    printf('abc')
Run Code Online (Sandbox Code Playgroud)

如果我运行test(),它不会打印任何内容。

目前我正在做一些愚蠢的事情:

cdef char s[80]
sprintf(s, 'something')
print s
Run Code Online (Sandbox Code Playgroud)

在 cython 中使用什么更好的方法printf?为什么不打印?

ost*_*ach 4

您可以使用该wurlitzer包捕获 C 级 stdout / stderr 并将其重定向到 IPython。

例如,在 Jupyter 笔记本中包含以下代码块:

%load_ext Cython
%load_ext wurlitzer
Run Code Online (Sandbox Code Playgroud)
%%cython
from libc.stdio cimport printf
def test():
    printf('abc')
Run Code Online (Sandbox Code Playgroud)
test()
# prints "abc"
Run Code Online (Sandbox Code Playgroud)