美文网首页Python开发实战
Python 3 查看字符编码方法

Python 3 查看字符编码方法

作者: Young先生 | 来源:发表于2019-02-21 13:58 被阅读0次

查看字符编码,需要用到chardet模块

一、查看网页编码

#coding=utf-8
import urllib.request
import chardet
url = 'http://www.baidu.com'
a = urllib.request.urlopen(url)
encode = chardet.detect(a.read())
print(encode['encoding'])

二、查看文件内容编码

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

三、查看某个字符串编码

import chardet
s = '张三'
print(chardet.detect(str.encode(s)))
输出信息:{'encoding': 'utf-8', 'confidence': 0.7525, 'language': ''}

Tips:

chardet.detect 在查看字符串传的编码时,必须要把字符串encode后,才能查看当前字符串编码格式

欢迎大家访问我的博客:bigyoung.cn

欢迎大家关注我的公众号:


公众号二维码.jpg

相关文章

  • python 高级方法

    Python的字符串类型 字符编码方法 查看Python中的字符串编码名称,查看系统的编码 源文件字符集编码声明:...

  • Python 3 查看字符编码方法

    查看字符编码,需要用到chardet模块 一、查看网页编码 二、查看文件内容编码 三、查看某个字符串编码 Tips...

  • python零基础入门到实战,基础知识总结(一)

    1编写第一个程序(python 3) 2.查看python 版本号 3.使用变量 4.字符串 5.合并拼接字符串 ...

  • Python数组

    python查看字符串中指定字符非重叠出现的次数 python反转数组内容 Python数组排序1 python数...

  • python 3 关键字

    查看python3的所有关键字(其中nonlocal是python3新增) 检查字符串是否为关键字,返回True或...

  • 前端Base64

    Base64 Base64是一种编码方法,可以将任意字符转成可打印字符。使用这种编码方法,主要不是为了加密,而是为...

  • Mac修改默认Python版本

    1.安装Python 3 2.查看Python3 安装路径 打开终端,输入which python3,查看路径:/...

  • Python的彻底卸载

    1、查看python3版本 python3--version 2、查看并删除Python 3.7 框架 $ ls ...

  • python终端命令

    查看python版本: python -V python3 -V 查看python安装路径系统自带pyth...

  • Linux⑤ | CentOS Linux实践-python2和

    安装Python2和Python3共存: 1、查看python版本python -v 2、查看Python可执行文...

网友评论

    本文标题:Python 3 查看字符编码方法

    本文链接:https://www.haomeiwen.com/subject/gfpryqtx.html