Pythonは、ログファイルのエラー「UnicodeDecodeError」を読んで

問題の説明:

私は、Pythonスクリプトは、ログファイルを読み込み、書きました:

# -*- coding:utf-8 -*-
import os
import numpy as np
file = 'D:\pythonfile\test.log'

for line in open("test.log","r"):
    print(line)

しかし、実行中のエラー:
実行中のコードのエラー:

Traceback (most recent call last):
  File "D:/pythonfile/my-test225.py", line 8, in <module>
    for line in open("test.log","r"):
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 6946: illegal multibyte sequence

Process finished with exit code 1

エラー図:
Pythonは、ログファイルのエラー「UnicodeDecodeError」を読んで

問題の原因:

フォーマットを符号化及び復号化フォーマット読み取り履歴ログは矛盾に起因するためであります

問題解決:

この方法は、ファイルが「エンコーディングを指定= 『UTF-8』読み取ります。

# -*- coding:utf-8 -*-
import os
import numpy as np
file = 'D:\pythonfile\test.log'

for line in open("test.log","r",encoding='UTF-8'):
    print(line)

第二の方法は、ファイル指定されたRB(RBバイナリ読み出しモードでオープン)を読み取るします。

# -*- coding:utf-8 -*-
import os
import numpy as np
file = 'D:\pythonfile\test.log'

# for line in open("test.log","rb"):
    print(line)

おすすめ

転載: blog.51cto.com/10950710/2476370