日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 测试字符串类型_【教程】如何用Python中的chardet去检测字符编码类型

發布時間:2024/10/6 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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去检测字符编码类型的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。