在Windows上使用Python 3.4.3。Python popen() - 溝通(str.encode(encoding =「utf-8」,errors =「ignore」))崩潰
我的腳本運行在控制檯中的小Java程序,並應得到輸出繼電器:
import subprocess
p1 = subprocess.Popen([ ... ], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
out, err = p1.communicate(str.encode("utf-8"))
這導致正常
'UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 135: character maps to < undefined>'.
現在我想忽略錯誤:
out, err = p1.communicate(str.encode(encoding="utf-8", errors="ignore"))
這導致了一個更有趣的錯誤,我發現沒有幫助使用谷歌:
TypeError: descriptor 'encode' of 'str' object needs an argument
因此,似乎python甚至不知道str.encode(...)的參數是什麼。當你忽略錯誤部分時也是如此。
問題是:如果你使用通信(str.encode(「utf-8」)),這工作正常(可以看到幾個stackoverflow的例子),除了一些unicode錯誤。但是,當您添加錯誤參數或使用encoding =「utf-8」時,它會中斷。否則,它工作正常。 – user136036
這是因爲'std.encode(「utf-8」)'的確和''utf-8「.encode()相同,它會返回...」utf-8「:)所以你實際上發送文本「utf-8」到您的應用程序 - 我不認爲這就是您想要做的。 – viraptor