Python 把包含\\u4f20\\u5a92 unicode内容的字典字符串变成字典

发布时间 2023-12-21 15:40:12作者: shaomine
import json
# 把包含\\u4f20\\u5a92  unicode内容的字典字符串变成字典
def text_to_dict(text):
    dict1 = json.loads(text)
    str_dict = str(dict1).replace('\\xa0','').replace('\'','"')
    dict_json = json.loads(str_dict)
    return dict_json

if __name__ == "__main__":
    text='{"delight_ranges":[],"image_ranges":[],"inline_style_ranges":[],"aggregated_ranges":[],"ranges":[],"color_ranges":[],"text":"\\u4f20\\u5a92\\u516c\\u53f8 \\u00b7 73\\u00a0\\u4e07\\u4f4d\\u7c89\\u4e1d \\u00b7 \\u8fc7\\u53bb 2 \\u5468\\u5185\\u53d1\\u5e03\\u4e86 10+ \\u7bc7\\u5e16\\u5b50"}'
    dict_json = text_to_dict(text)

    print(dict_json)
    # {'delight_ranges': [], 'image_ranges': [], 'inline_style_ranges': [], 'aggregated_ranges': [], 'ranges': [], 'color_ranges': [], 'text': '传媒公司 · 73万位粉丝 · 过去 2 周内发布了 10+ 篇帖子'}
    print(1)