科学の箱

科学・IT・登山の話題

Python

cp932 error

投稿日:2018年11月7日 更新日:

pythonで入力ファイルにおける文字コードが正しく認識されない場合、以下のようなエラーがでる。

UnicodeDecodeError: ‘cp932’ codec can’t decode byte 0x98 in position 8107: illegal multibyte sequence

このエラーはUTF-8のファイルをcp932で読み込む場合に起きやすい。

対応法方法としては2つある。

  1. 入力ファイルをSJISに変換
  2. 入力ファイルをbinaryモードで読み込み、適切なdecodeをかける。

2は例えば以下のようなコードになる。

messages = [line.decode(‘utf-8’).rstrip() for line in open(‘smsspamcollection/SMSSpamCollection’, ‘rb’)]

メタ情報

inarticle



メタ情報

inarticle



-Python

執筆者:


comment

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

関連記事

no image

automated the boring – day7

https://automatetheboringstuff.com/chapter14/ 14章ではcsvとJSONを取り扱う。フォーマットとしては単純であるのに、なぜexcel,word, pdf …

no image

automated the boring – day6

https://automatetheboringstuff.com/chapter11/ さてこの辺からようやく面白くなってくる。webscrapingをするにあたり、まずはブラウザをpythonか …

no image

GCP google translatorの利用

こちらが詳しい https://cloud.google.com/translate/docs/reference/libraries#client-libraries-usage-python &n …

no image

automated the boring – day8

https://automatetheboringstuff.com/chapter15/ さてプログラムを実行しているときに案外出てくる要件が時間計測。 例えばアルゴリズム間でパフォーマンスを比較す …

no image

pythonでjanomeを使う

PythonでMecabを利用して形態要素解析に挑戦したところMecabが原因不明のエラーを引き起こしいったん断念。 代わりにJanomeを導入してみた。 Janomeは下記からインストーラを取得する …

2018年11月
« 10月   12月 »
 1234
567891011
12131415161718
19202122232425
2627282930  

side bar top



アーカイブ

カテゴリー