简体   繁体   English

从IPython捕获stdout时输出损坏/乱码

[英]Corrupted / Garbled Output when capturing stdout from IPython

A similar question was asked here: ipython redirect stdout display corruption , but the answer is not satisfactory. 在这里提出了类似的问题: ipython redirect stdout显示损坏 ,但是答案并不令人满意。

What I'm trying to do is capture standard output, write it to a PyQt4 QEditText while also sending it to the terminal as normal. 我想做的是捕获标准输出,将其写入PyQt4 QEditText,同时也将其正常发送给终端。

I'm using python 2.7 and Ubuntu 12.04 LTS. 我正在使用python 2.7和Ubuntu 12.04 LTS。

The problem is that when I embed an IPython terminal inside the program, the terminal output text gets garbled and I lose autocomplete ability. 问题是,当我在程序中嵌入IPython终端时,终端输出文本会出现乱码,并且会失去自动完成功能。

I was able to reproduce this issue very simply on my Ubuntu machine (the problem doesnt seem to exist on windows). 我能够非常简单地在Ubuntu计算机上重现此问题(Windows上似乎不存在该问题)。

In my ubuntu termianl: 在我的Ubuntu术语中:

ipython 
import IPython.utils.io
tee = IPython.utils.io.Tee('fds')  

right here the text gets garbled while simply trying to use the Tee object. 在这里,仅尝试使用Tee对象时,文本就会出现乱码。

In my program I do things a little differently, but it boils down to overwriting sys.stdout with a custom object and then when write or flush gets called I log the captured text and send it back to the original stdout and the PyQt4 gui. 在我的程序中,我的操作有所不同,但是归结为使用自定义对象覆盖sys.stdout,然后当调用write或flush时,我记录了捕获的文本并将其发送回原始stdout和PyQt4 gui。


So, is there a way to safely peek at standard out whenever the write or flush command gets called? 因此,有没有一种方法可以在调用write或flush命令时安全地窥视标准输出? And is there a way to do this that doesn't break IPython terminals? 有没有办法做到这一点不会破坏IPython终端?

It's due to special encoding for coloring of text in the terminal. 这是由于终端中文本着色的特殊编码所致。 Try using 'ipython --classic'. 尝试使用'ipython --classic'。

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

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