Python 3-解码转义的utf-16字符串

问题描述 投票:0回答:1

我正在研究简单的python脚本。不幸的是,我必须使用的一些数据存储如下:

我的数据

trouble_string = '{\"N\": \"Centr\\u00e1lna nervov\\u00e1 s\\u00fastava\"}'

我要实现的目标

我想以以下格式转换字符串。

decoded_string = '{"N": "Centrálna nervová sústava"}'

您会看到带有重音字母的数字编码。有什么聪明的方法可以解码此字符串吗?

我尝试过的

bytes(s, encoding='utf-8').decode(encoding='utf-16') # outputs: '䌢湥牴畜〰ㅥ湬\u2061敮癲癯畜〰ㅥ猠畜〰慦瑳癡≡' bytes(s, encoding='utf-16').decode(encoding='utf-8') # outputs: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

python encoding utf-8 utf-16
1个回答
1
投票
trouble_string = '{\"N\": \"Centr\\u00e1lna nervov\\u00e1 s\\u00fastava\"}' result = trouble_string.encode().decode("unicode-escape")
© www.soinside.com 2019 - 2024. All rights reserved.