0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[pytohn]テキストファイルの文字コード特定

0
Last updated at Posted at 2026-02-19
import chardet

with open(r'C:\xxxxx\yyyyy.csv', 'rb') as f:
    result = chardet.detect(f.read())
    print(result)

detectはDict型(辞書型)を返す。

{'encoding': 'SHIFT_JIS', 'confidence': 0.99, 'language': 'Japanese'}
項目 データ
encoding 検出された文字エンコーディング名を示す文字列です。
open関数のencodingにはこれをそのまま渡せばよい。
confidence 文字コードの判定結果に対する信頼度(確信度) を表す数値です。
0.0 から 1.0 の範囲の浮動小数点数で示され、1.0 に近いほど「その文字コードで間違いない」という確信が高いことを意味します。
language 検出された言語を示す文字列です。

Windowsの日本語ファイルを扱う場合は shift_jis より cp932 を指定する方が無難。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?