首页 > 解决方案 > 如何在 Python 中将十六进制+字符转换为字符

问题描述

Str = """\\x3Cstyle\\x3E\\x0A\\x20\\x20\\x20.mainDiv\\x0A\\x20\\x20\\x7B\\x0A\\x20\\x20width\\x3A1000px\\x3B\\x0A\\x20\\x20background\\x2Dimage\\x3Aurl"""

我需要输出为:

<style\> 
.mainDiv
{
width\:1000px;
background-image:URL

}

我需要使用 Python 对其进行解码

标签: javascriptpythoncssencodingutf-8

解决方案


你必须'raw_unicode_escape'使用'unicode_escape'

Str = """\\x3Cstyle\\x3E\\x0A\\x20\\x20\\x20.mainDiv\\x0A\\x20\\x20\\x7B\\x0A\\x20\\x20width\\x3A1000px\\x3B\\x0A\\x20\\x20background\\x2Dimage\\x3Aurl"""

Str = Str.encode('raw_unicode_escape').decode('unicode_escape')

print(Str)

Python 文档:编解码器Python 特定编码


推荐阅读