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设计】。

相关文章

对django layer弹窗组件的使用详解

父层: <div class="col-xs-12"> <div class="box"> <div class="box-header...

使用TensorFlow-Slim进行图像分类的实现

参考 https://github.com/tensorflow/models/tree/master/slim 使用TensorFlow-Slim进行图像分类 准备 安装Tensor...

python实现dict版图遍历示例

复制代码 代码如下:#_*_coding:utf_8_import sysimport os class Graph():    def __init__(...

Python更新数据库脚本两种方法及对比介绍

最近项目的两次版本迭代中,根据业务需求的变化,需要对数据库进行更新,两次分别使用了不同的方式进行更新。 第一种:使用python的MySQLdb模块利用原生的sql语句进行更新 im...

Selenium定时刷新网页的实现代码

代码 代码很简单,主要是为了熟悉Selenium这个库的函数,为后续的短信轰炸做个铺垫 from selenium import webdriver import time imp...