我们在做Python爬虫抓取网页信息时常遇到中文的unicode编码问题,如“\u4e2d\u534e\u4eba\u6c11\u5171\u548c\u56fd”需要转换为中文,实际上这是“中华人民共和国”的unicode编码。Python语言可用以下方法转换:
#!/usr/bin/env python data = "中华人民共和国" encode_data = data.encode("unicode_escape") print(encode_data) # b'\\u4e2d\\u534e\\u4eba\\u6c11\\u5171\\u548c\\u56fd' decode_data = encode_data.decode("unicode_escape") print(decode_data) # 中华人民共和国