Python获取字符串的编码

chardet可以来帮助我们检测字符串的编码类型,使用如下:

  1. s ="utf-8 字符串"
  2. chardet.detect(s)

得到的结果如:

  1. {'confidence':0.87624999999999997,'encoding':'utf-8'}

这里需要提醒一下,因为字符串本身就有可能包含多种编码,因此chardet给出一个可信度(confidence),需要用户自己去判断和使用。但不管怎么,有总比没有强。

原文地址:https://www.cnblogs.com/feisky/p/2806972.html