科学の箱

科学・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

まずは肩慣らし print(‘Hello world!’) print(‘What is your name?’) # ask for their na …

no image

kaggle Titanic Tutorial – 10

いろいろ試しているがうまくいかないので、とりあえずAgeを正しく補完できるか調べる。 調査は線形回帰でどれくらい相関が出るかで判断する。   import numpy as nm impor …

no image

python coding styleのツール

Pythonで使えるコーディングツール pep8 flake8 pylint   Related posts:Python + Slack Bot – 2pythonでsha3をつかうPyt …

no image

automated the boring – day9

https://automatetheboringstuff.com/chapter17/ >>> from PIL import Image &gt …

no image

pythonでjanomeを使う

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

2018年11月
月 火 水 木 金 土 日
« 10月   12月 »
 1234
567891011
12131415161718
19202122232425
2627282930  

side bar top



アーカイブ

カテゴリー