Pythonパッケージ「chardet」でファイルの文字コードを確認する方法についてまとめました。
【chardet】ファイルの文字コードを確認
Pythonでは「chardet」モジュールを使って簡単にファイルの文字コードを判別できます。
ただし、標準モジュールではないのでpipなどでインストールする必要があります。
pip install chardet
サンプルコード
サンプルプログラムのソースコードです。
# -*- coding: utf-8 -*-
import chardet
with open("test.txt", "rb") as f:
char_code = chardet.detect(f.read())
print(char_code) #{'encoding': 'utf-8', 'confidence': '', 'language': ''}
| – | 関連記事 |
|---|---|
| 1 | ■【WinPython】使い方・設定まとめ |
| 2 | ■Python入門 基礎文法とサンプル集 |

コメント