zoukankan      html  css  js  c++  java
  • chardet 模块

    #coding:utf-8
    #指定本文件编码为utf-8
    #python 27
    #xiaodeng
    #chardet模块
    
    #chardet模块下载地址:
    #1)http://pan.baidu.com/s/1gdfOH95
    #2)登录python官网下载
    
    
    import os,chardet
    filename=r'D:测试文件.txt'
    
    #怎么判断文件/string的编码格式?
    #在处理字符串时,常常会遇到不知道字符串是何种编码,如果不知道字符串的编码就不能将字符串转换成需要的编码,
    #如此,chardet产生,是一个非常优秀的编码识别模块。
    
    
    #1)chardet.detect()方法判断编码格式
    #chardet可以直接用detect函数来检测所给字符的编码。
    print chardet.detect(filename)              #{'confidence': 0.938125, 'encoding': 'utf-8'}
    fp=open(filename.decode('utf-8'),'r')
    fp=fp.read()
    
    
    
    #2)detect()方法返回值:
    #chardet.detect()方法返回一个字典,confidence是精确度,encoding是编码格式
    #如:
    ##{'confidence': 0.938125, 'encoding': 'utf-8'},confidence表示编码为utf-8的概率为93.81%
  • 相关阅读:
    共享经济
    滑动用hammer
    js 数组去重 的5种方法
    js ajax上传图片到服务器
    js url图片转bese64
    去除移动端 a标签 点击有一个 阴影效果
    css 文字超出变 ... 点点点
    h5手势库 hammer.js
    xshell linux传文件
    IO流(Properties存取)
  • 原文地址:https://www.cnblogs.com/dengyg200891/p/4940090.html
Copyright © 2011-2022 走看看