代码:
from ctypes import *
libc = cdll.LoadLibrary('msvcrt.dll')
libc.printf(c_char_p(b'Hello %d %.2f\n'), c_int(16), c_double(2.3))
在标准交互环境中显示内容为:
Hello 16 2.30
14
在 ipython 中,显示内容为:
Hello 16 2.30
Out[31]: 14
在 jupyter 中,显示内容为:
Out[15]: 14
请问在 jupyter 中如何显示“Hello 16 2.30”
1
XIVN1987 OP 找到一篇文章,上面说出现这种问题的原因是:
IPython forwards the Python-level sys.stdout and sys.stderr, but it leaves the process-level file descriptors that C code will write to untouched. That means that in a context like this notebook, these functions will print to the terminal, because they are not captured. 文章链接: https://notebook.community/minrk/wurlitzer/Demo |
2
XIVN1987 OP 虽然在 jupyter notebook 中没有显示“Hello 16 2.30”,但在 jupyter.exe 终端中有显示,,能够验证代码的执行效果,,所以这个问题不算很严重
|