【教程】如何用Python中的chardet去检测字符编码类型
来源:互联网 发布:淘宝号被天猫蚁盾 编辑:程序博客网 时间:2024/06/03 18:13
【背景】
之前已经使用过chardet了,也算用了不少次了。
之前也写过和chardet相关的:
【已解决】windows下,安装python的chardet
【问题】Python中用Chardet检测出来从Windows的cmd中输入的字符串的编码是KOI8-R,而不是所希望的GBK或GB2312
【整理】Python的字符编码检测库:charade和chardet的区别
但是没写教程,举例说明如何使用。
现在去举例解释解释。
【python示例代码演示如何用chardet检测字符串的编码类型】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/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)= <type 'dict'>
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,算是很好用的了。
阅读全文
0 0
- 如何用Python中的chardet去检测字符编码类型
- 【教程】如何用Python中的chardet去检测字符编码类型
- Python字符编码检测 -- chardet
- python字符编码检测——chardet
- Python使用chardet检测字符编码
- 17.python字符编码检测——chardet
- Python之检测文件的字符编码(chardet)
- python编码检测模块chardet
- Python chardet 字符编码判断
- python-字符编码库chardet
- Python chardet 字符编码判断
- python chardet模块 检测文档编码
- Python之动态检测编码chardet
- Python编码检测 chardet 效果不如 UnicodeDammit
- Python基础 检测多种语言编码 chardet
- Python中使用chardet判断字符编码
- python使用模块chardet判断字符编码
- 【整理】Python的字符编码检测库:charade和chardet的区别
- 迁移到Android Studio 3.0
- 我的文章
- 初论差分
- 模板方法模式
- JAVA线程与线程通信,进程与进程通信
- 【教程】如何用Python中的chardet去检测字符编码类型
- FatJar减肥: 解决SpringBoot jar包太大的问题
- 数据结构——树(1)——二叉堆
- 【web自动化测试-Robotframework-翻页功能之二】
- dubbo源码 学习笔记(五)
- opencv3.0+VS2013+opencv_contrib+cmake3.4.3+win8配置方法
- 线程操作wait和notify
- 5.超强力方法
- Android OpenGL ES 2.0 (七) FramebufferObject(FBO)