2017-05-02 57 views
0

我試圖通過行改爲子行:從TextIOWrapper讀引起的UnicodeDecodeError

proc = subprocess.Popen(self.monitor_logcat_cmd, shell=True, stdout=subprocess.PIPE, 
         bufsize=1, universal_newlines=True) 

while proc.poll() is None: 
    line = proc.stdout.readline() 
    print("Process line: " + str(line)) 

它的工作原理,但在某些時候,我得到錯誤:

Exception in thread Thread-14: 
Traceback (most recent call last): 
    File "/Users/F1sherKK/anaconda3/lib/python3.6/threading.py", line 916, in _bootstrap_inner 
    self.run() 
    File "/Users/F1sherKK/Dev/Python/AutomationTestSupervisor/session/SessionThreads.py", line 46, in run 
    line = proc.stdout.readline() 
    File "/Users/F1sherKK/anaconda3/lib/python3.6/codecs.py", line 321, in decode 
    (result, consumed) = self._buffer_decode(data, self.errors, final) 
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc0 in position 89: invalid start byte 

有什麼辦法來添加/指定編碼子進程的標準輸出?我想補充錯誤「忽略」。

有沒有其他方法可以解決這個問題?

+0

那麼過程產生什麼字節,關掉'universal_newlines'?你知道過程產生什麼編碼嗎? –

+0

'monitor_logcat_cmd'包含什麼?什麼命令在shell中運行?你是否設置了「LANG」或「LC_CTYPE」環境變量? –

+0

monitor_logcat_cmd是'adb -s 5554 logcat'它實時從Android設備讀取日誌。它可以包含我猜測的各種編碼。例如,日誌中可以有表情符號。我沒有設置任何env變量。 – F1sher

回答

0

可能剛剛設置的errors關鍵字參數Popen()'ignore'。從documentation

If encoding or errors are specified, or universal_newlines is true, the file objects stdin, stdout and stderr will be opened in text mode using the encoding and errors specified in the call or the defaults for io.TextIOWrapper .

但是,很顯然你的過程中不使用UTF-8的編碼輸出。您可能想要弄清楚a)它可以配置爲產生不同的編碼,或者b)使用什麼編碼並將其配置(使用關鍵字參數爲Popen())。

+0

謝謝。我用'errors =「ignore」'得到了臨時解決方案。我之前沒有嘗試過,因爲PyCharm在嘗試使用它時發生了「意外的爭論」錯誤。但它有效。我認爲可能很難爲此日誌設置單一編碼。我認爲它應該是UTF-8,但有時單個元素不是UTF-8。我不是編碼專家,但這就是我對此的理解。在我的情況下,我需要從設備上保存20k行日誌,所以如果他們中很少有人會「忽略錯誤」,我不認爲這會對我造成任何問題。儘管我用各種編碼來填充實驗。謝謝。 – F1sher