在 Python 中解码双编码的 utf8 [英] Decoding double encoded utf8 in Python

查看:27
本文介绍了在 Python 中解码双编码的 utf8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过 xmlrpc 从我的一个客户那里得到的字符串有问题.他向我发送了两次编码的 utf8 字符串 :( 所以当我在 python 中获取它们时,我有一个必须再解码一次的 unicode 对象,但显然 python 不允许这样做.我已经注意到我的客户,但是我需要在他修复之前先快速解决.

I've got a problem with strings that I get from one of my clients over xmlrpc. He sends me utf8 strings that are encoded twice :( so when I get them in python I have an unicode object that has to be decoded one more time, but obviously python doesn't allow that. I've noticed my client however I need to do quick workaround for now before he fixes it.

来自 tcp 转储的原始字符串:

Raw string from tcp dump:

<string>Rafaxc3x85xc2x82</string>

这被转换成:

u'Rafaxc5x82'

我们得到的最好的是:

eval(repr(u'Rafaxc5x82')[1:]).decode("utf8") 

这导致正确的字符串是:

This results in correct string which is:

u'Rafau0142' 

然而这很丑陋,不能在生产代码中使用.如果有人知道如何以更合适的方式解决这个问题,请写信.谢谢,克里斯

this works however is ugly as hell and cannot be used in production code. If anyone knows how to fix this problem in more suitable way please write. Thanks, Chris

推荐答案


>>> s = u'Rafaxc5x82'
>>> s.encode('raw_unicode_escape').decode('utf-8')
u'Rafau0142'
>>>

这篇关于在 Python 中解码双编码的 utf8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆