python 测试字符串类型_【教程】如何用Python中的chardet去检测字符编码类型
【背景】
之前已經使用過chardet了,也算用了不少次了。
之前也寫過和chardet相關的:
但是沒寫教程,舉例說明如何使用。
現在去舉例解釋解釋。
【python示例代碼演示如何用chardet檢測字符串的編碼類型】#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:
【教程】如何用Python中的chardet去檢測字符編碼類型
https://www.crifan.com/python_chardet_example_show_how_to_use_to_detect_charset
Version: 2013-09-16
Author: Crifan Li
Contact: https://www.crifan.com/contact_me/
"""
import chardet;
def chardet_detect_str_encoding():
"""
Demo how to use chardet to detect string encoding/charset
"""
inputStr = "當前文件時UTF-8,所以你所看到的這段字符串,也是UTF-8編碼的";
detectedEncodingDict = chardet.detect(inputStr);
print "type(detectedEncodingDict)=",type(detectedEncodingDict); #type(detectedEncodingDict)=
print "detectedEncodingDict=",detectedEncodingDict; #detectedEncodingDict= {'confidence': 0.99, 'encoding': 'utf-8'}
detectedEncoding = detectedEncodingDict['encoding'];
print "That is, we have %d%% confidence to say that the input string encoding is %s"%(int(detectedEncodingDict['confidence']*100), detectedEncoding);
if __name__ == '__main__':
chardet_detect_str_encoding();
【總結】
相對來說,chardet,算是很好用的了。
總結
以上是生活随笔為你收集整理的python 测试字符串类型_【教程】如何用Python中的chardet去检测字符编码类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用python读取文档_python读取
- 下一篇: python读取word指定内容_pyt