python解码未知编码格式,使用Python解码未知的编码繁体中文字符串

博客讨论了如何处理一种特殊的编码问题,即网站统计数据中出现的无法理解的搜索字符串。这个字符串是由于URL编码后的UTF-8字节被误解释为拉丁-1编码导致的。通过使用Python的`urllib2.unquote`和`decode('utf8')`方法,可以成功将乱码字符串转换回正确的“台南親子餐廳”。这个问题涉及到字符编码的识别和转换,对于网页开发和数据解析具有重要意义。
摘要由CSDN通过智能技术生成

Hi I have a website that is in Traditional Chinese and when I check the site statistics it tell me that the search term for the website is å%8f°å%8d%97 親å­%90é¤%90廳 which obviously makes no sense to me. My question is what is this encoding called? And is there a way to use Python to decode this character string. Thank you.

解决方案

It is called a mutt encoding; the underlying bytes have been mangled beyond their original meaning and they are no longer a real encoding.

It was once URL-quoted UTF-8, but now interpreted as latin-1 without unquoting those URL escapes. I was able to un-mangle this by interpreting it as such:

>>> from urllib2 import unquote

>>> bytesquoted = u'å%8f°å%8d%97 親å­%90é¤%90廳'.encode('latin1')

>>> unquoted = unquote(bytesquoted)

>>> print unquoted.decode('utf8')

台南 親子餐廳

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值