社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python 编码问题

yi4534 • 6 年前 • 1059 次点击  
#encoding='utf-8' 
import tstEncode
import chardet


strsrc = 'abcd在1efg'
rt1= chardet.detect(strsrc)
print rt1

strsrc = strsrc.decode(rt1['encoding'])
strsrc =strsrc.encode('GBK','ignore')
rt1= chardet.detect(strsrc)
print rt1

为什么这个编码会这样: {'confidence': 0.73, 'language': '', 'encoding': 'Windows-1252'} {'confidence': 0.73, 'language': '', 'encoding': 'ISO-8859-1'}

本应该有的结果不是:前是 UTF-8 后面得到的应该为GBK才对啊! 求解

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/2222
 
1059 次点击  
文章 [ 1 ]  |  最新文章 6 年前
Py站长
Reply   •   1 楼
Py站长    6 年前

strsrc = 'abcd在1efg' 中的 1e 是特殊字符,会有问题。

另外,最好不要在代码里有中文并且转码转来转去的。各种问题的。统一用utf-8就好了。

相关 转码的文章在这里 http://in355hz.iteye.com/blog/1860787

# coding=utf-8

import chardet

strsrc = "abcd在在在在1fg"

rt1 = chardet.detect(strsrc)
print rt1

import urllib

rawdata = urllib.urlopen('http://chardet.readthedocs.io/en/latest/usage.html').read()
print chardet.detect(rawdata)