1 | pip install --upgrade chardet |
1 | chardet.detect(aBuf) -> dict |
1234567891011121314151617181920212223 | #!/usr/bin/env python # -*- coding: utf-8 -*- # @Date : 2016-11-23 11:14:15 # @Author : 李满满 (xmdevops@vip.QQ.com) # @Link : http://xmdevops.blog.51cto.com/ # @Version : $Id$ from __future__ import absolute_import # 说明: 导入公共模块 import os import chardet # 说明: 导入其它模块 if __name__ = = '__main__' : res_lines = [] with open ( 'ChangeLog_Chinese.dat' , 'r+b' ) as fd: res_line = os.linesep for line in fd: line = line.lstrip() encoding = chardet.detect(line).get( 'encoding' , None ) PRint encoding if encoding: res_line = line.decode(encoding, 'replace' ).encode( 'utf-8' ) res_lines.append(res_line) print res_lines |
新闻热点
疑难解答