且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python3,如何正确编码此字符串?

更新时间:2023-11-28 23:47:10

如果您有Unicode字符串"<h1>Sacr\xc3\xa9 bleu!</h1>",则说明出现了问题.您的输入被破坏了,或者在处理时做错了什么.例如,在这里,您已将Python 2示例复制到Python 3解释器中.

If you have the Unicode string "<h1>Sacr\xc3\xa9 bleu!</h1>", something has already gone wrong. Either your input is broken, or you did something wrong when processing it. For example, here, you've copied a Python 2 example into a Python 3 interpreter.

如果因为弄错了字符串而使它断了,那么您应该真正修复因为做错了的事情.如果仍然需要将"<h1>Sacr\xc3\xa9 bleu!</h1>"转换为b"<h1>Sacr\xc3\xa9 bleu!</h1>",则将其编码为latin-1:

If you have your broken string because you did something wrong to get it, then you should really fix whatever it was you did wrong. If you need to convert "<h1>Sacr\xc3\xa9 bleu!</h1>" to b"<h1>Sacr\xc3\xa9 bleu!</h1>" anyway, then encode it in latin-1:

bytestring = broken_unicode.encode('latin1')