问题
%%cython
from libc.stdio cimport printf
def test():
printf('abc')
If I run test()
, it doesn't print anything.
Currently I am doing something stupid as:
cdef char s[80]
sprintf(s, 'something')
print s
What's a better way to use printf
in cython? Why doesn't it print?
回答1:
You can use the wurlitzer package to capture C-level stdout / stderr and redirect it to IPython.
For example, include the following code blocks in your Jupyter notebook:
%load_ext Cython
%load_ext wurlitzer
%%cython
from libc.stdio cimport printf
def test():
printf('abc')
test()
# prints "abc"
回答2:
Output will be printed in your terminal (console) running Jupyter notebook. not sure how to display it in Jupyter though.
来源:https://stackoverflow.com/questions/29262667/how-to-allow-c-printf-to-print-in-ipython-notebook-in-cython-cell