Python3 chardet模块查看编码格式的例子

yipeiwu_com6年前Python基础

如下所示:

需要注意的是,如果遇到GBK2312等编码的,在decode和encode时,一律使用GBK进行编码或者解码,这是因为GBK是其他GBK编码的超集,向下兼容所有的GBK编码。

下面是一个例子:

#coding=utf-8
import urllib.request
import chardet
url = 'http://www.baidu.com'

a = urllib.request.urlopen(url)

'''
chardet模块
使用该模块可以查看字符串的编码格式:chardet.detect()
'''
encode = chardet.detect(a.read())
print(encode['encoding'])

#假设存在一个a.txt的文件
f = open('a.txt', 'rb')
print(chardet.detect(f.read(100)))

以上这篇Python3 chardet模块查看编码格式的例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python在Windows下安装setuptools(easy_install工具)步骤详解

python在Windows下安装setuptools(easy_install工具)步骤详解

本文讲述了python在Windows下安装setuptools(easy_install工具)的方法。分享给大家供大家参考,具体如下: 【题外话介绍下setuptools】 setup...

python实现大战外星人小游戏实例代码

主程序 import pygame from pygame.sprite import Group from settings import Settings from game_...

利用soaplib搭建webservice详细步骤和实例代码

最近在搞基于python的webservice项目,今天为把环境给配好,折腾了不少时间,还是把配的过程记录下来,以后备用:首先你系统上要有python,这个不必说啦,我系统上用的是2.7...

Python-ElasticSearch搜索查询的讲解

Elasticsearch 是一个开源的搜索引擎,建立在一个全文搜索引擎库 Apache Lucene™ 基础之上。 Lucene 可能是目前存在的,不论开源还是私有的,拥有...

python实现ping的方法

本文实例讲述了python实现ping的方法。分享给大家供大家参考。具体如下: #!/usr/bin/env python #coding:utf-8 import os, sys,...