2013-02-20 107 views
4

我試圖瞭解如何創建自定義打印功能。 (使用Python 2.7)python:更改sys.stdout打印到自定義打印功能

import sys 
class CustomPrint(): 
    def __init__(self): 
     self.old_stdout=sys.stdout #save stdout 

    def write(self, text): 
     sys.stdout = self.old_stdout #restore normal stdout and print 
     print 'custom Print--->' + text 
     sys.stdout= self # make stdout use CustomPrint on next 'print' 
         # this is the line that trigers the problem 
         # how to avoid this?? 


myPrint = CustomPrint() 
sys.stdout = myPrint 
print 'why you make 2 lines??...' 

上面打印的代碼本安慰:

>>> 
custom Print--->why you make 2 lines??... 
custom Print---> 

>>> 

,我想只打印一行:

>>>  
1custom Print--->why you make 2 lines??... 
>>> 

,但無法弄清楚如何使這個自定義打印工作,我明白,有某種遞歸觸發第二個輸出到控制檯(我使用self.write,分配標準輸出self.write自己!)

我該如何做這項工作?或者是我的做法完全錯誤...

+0

相關:[將stdout重定向到Python中的文件?](http://stackoverflow.com/q/4675728/4279) – jfs 2015-01-27 11:27:57

回答

2

這不是遞歸。會發生什麼情況是您的write函數被調用兩次,一次是您期望的文本,第二次只有'\n'。試試這個:

import sys 
class CustomPrint(): 
    def __init__(self): 
     self.old_stdout=sys.stdout 

    def write(self, text): 
     text = text.rstrip() 
     if len(text) == 0: return 
     self.old_stdout.write('custom Print--->' + text + '\n') 

我在上面的代碼做的是我的新行字符添加到在第一次調用傳遞文本,並確保通過print語句做出的第二個電話,一個旨在打印新行,不打印任何東西。

現在試評出來的前兩行,並看看會發生什麼:

def write(self, text): 
     #text = text.rstrip() 
     #if len(text) == 0: return 
     self.old_stdout.write('custom Print--->' + text + '\n') 
+0

謝謝,它的工作方式應該是:)不知道\ n是在第二遍時添加的! sys.stderr.write(「:」。join(「{0:b}」.format(ord(c))for c in text))甚至添加了這一行只是爲了確保你的權利^^ – andsoa 2013-02-20 18:22:16

+1

有一個小使用逗號分隔的打印時出現問題:'print var1,var2'顯然,逗號爲每個'var'調用打印並抑制在前面的'var'上添加'\ n'!這可以通過在customPrint類中添加tmp var來解決,該類存儲文本,並且僅當文本以'\ n'結尾時纔打印。 – andsoa 2013-02-21 13:24:26

+0

@andsoa好的,感謝您分享這個優雅的解決方案,感謝分享 – piokuc 2013-02-21 13:39:04

1

怎麼樣做from __future__ import print_function。這樣您將使用Python3打印功能,而不是Python2的打印語句。然後你可以重新定義打印功能:

def print(*args, **kwargs): 
    __builtins__.print("Custom--->", *args, **kwargs) 

有一個問題,你必須開始使用打印功能。

+0

這將工作,但我的情況我需要改變一些類的打印輸出寫python 2.7 print:/ – andsoa 2013-02-20 18:04:13

3

一個解決辦法可能是使用上下文管理器,如果是本地化。

#!/usr/bin/env python 
from contextlib import contextmanager 
################################################################################ 
@contextmanager 
def no_stdout(): 
    import sys 
    old_stdout = sys.stdout 
    class CustomPrint(): 
     def __init__(self, stdout): 
      self.old_stdout = stdout 

     def write(self, text): 
      if len(text.rstrip()): 
       self.old_stdout.write('custom Print--->'+ text) 

    sys.stdout = CustomPrint(old_stdout) 

    try: 
     yield 
    finally: 
     sys.stdout = old_stdout 
################################################################################ 
print "BEFORE" 
with no_stdout(): 
    print "WHY HELLO!\n" 
    print "DING DONG!\n" 

print "AFTER" 

上述生產:

BEFORE 
custom Print--->WHY HELLO! 
custom Print--->DING DONG! 
AFTER 

該代碼將需要尤其整理。圍繞班級應該做什麼WRT將stdout設置回原來的樣子。

+0

! – andsoa 2013-02-21 13:14:58