Pythonプログラミング:utf-8 codec can’t decodeエラー

その買うを、もっとハッピーに。|ハピタス

caffe2 tutorial学習中に、以下のようなエラーに遭遇した。

      1 # Read the contents of the input protobufs into local variables
      2 with open(INIT_NET) as f:
----> 3     init_net = f.read()
      4 with open(PREDICT_NET) as f:
      5     predict_net = f.read()

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 1: invalid start byte

with open(INIT_NET) as f:の部分に問題があるのは火を見るより明らかで、このエラーは以下のように、’rb’を付け加えることで簡単に修正できる。

with open(INIT_NET, 'rb') as f:
    init_net = f.read()
with open(PREDICT_NET, 'rb') as f:
    predict_net = f.read()

with openを使ってfileをopenする場合、’rb’や’rw’等を付け加える場面に多く出会す。

スポンサーリンク
スポンサーリンク