Python 3 查看字符编码方法

栏目: Python · 发布时间: 5年前

内容简介:查看字符编码,需要用到chardet模块一、查看网页编码二、查看文件内容编码

查看字符编码,需要用到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后,才能查看当前字符串编码格式
 

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

Python 3 查看字符编码方法
#####         如果我的文档给您提供了帮助,请点击一下页面广告,给个支持! #####如有需要沟通的,请直接评论或者是邮件沟通。欢迎小主关注我的微信公众号:BigYoung成长之路,微信搜索可关注。如想交个朋友,可直接加入我的QQ群:615870353;或者加入我的知识星球:ID:45959491

BigYoung版权所有,转载请邮件(Mr_wang_yang@163.com)获取授权,并注明转自:http://www.bigyoung.cn/1114.html


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

CSS3 Solutions

CSS3 Solutions

Marco Casario / Apress / 2012-8-13 / GBP 35.50

CSS3 brings a mass of changes, additions, and improvements to CSS across a range of new modules. Web designers and developers now have a whole host of new techniques up their sleeves, from working wit......一起来看看 《CSS3 Solutions》 这本书的介绍吧!

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具