如何让c printf在cython单元的ipython笔记本中打印? [英] How to allow c printf to print in ipython notebook in cython cell?

查看:288
本文介绍了如何让c printf在cython单元的ipython笔记本中打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

%%cython
from libc.stdio cimport printf
def test():
    printf('abc')

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

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

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

What's a better way to use printf in cython? Why doesn't it print?

推荐答案

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

You can use the wurlitzer package to capture C-level stdout / stderr and redirect it to IPython.

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

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"

这篇关于如何让c printf在cython单元的ipython笔记本中打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆