python 把包含uincode字符串变成中文

 1 def get_info_by_pattern(text, pattern):
 2     p = re.compile(pattern)
 3     p_res = p.findall(text)
 4     return p_res
 5 
 6 # 把包含uincode字符串变成中文
 7 def unicode_to_chinese(text):
 8     pattern_unicode = 'u[0-9a-z]{4}'
 9     p_res = get_info_by_pattern(text, pattern_unicode)
10     if len(p_res) > 0:
11         list1 = []
12         for ucode in p_res:
13             unicode_str = '\\' + ucode
14             if unicode_str in text and unicode_str not in list1:
15                 list1.append(unicode_str)
16         for ucode in list1:
17             unicode_str = ucode
18             try:
19                 chinese_str = bytes(unicode_str, 'utf-8').decode('unicode_escape')
20                 # print(unicode_str, chinese_str)
21                 text = text.replace(unicode_str, chinese_str)
22             except:
23                 pass
24     return text

text='text=":[]"aggregated_ranges":[]"ranges":[]"color_ranges":[]"text":"\\u5730\\u65b9\\u6027\\u5546\\u5bb6 \\u00b7 2 位粉丝"}"'
a=unicode_to_chinese(text)
print(a
输出:text=":[]"aggregated_ranges":[]"ranges":[]"color_ranges":[]"text":"地方性商家 · 2 位粉丝"}"

posted on 2023-12-25 10:54  shaomine  阅读(14)  评论(0编辑  收藏  举报