掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
我們使用python中,遇到爬取網(wǎng)站情況,用到unicode編碼,我們需要將它轉(zhuǎn)換為中文,unicode編碼轉(zhuǎn)換為中文的方法有四種:使用unicode_escape 解碼、使用encode()方法轉(zhuǎn)換,再調(diào)用bytes.decode()轉(zhuǎn)換為字符串形式、 使用json.loads 解碼(為json 格式)、使用eval(遇到Unicode是通過requests在網(wǎng)上爬取的時(shí)候)。具體內(nèi)容請(qǐng)看本文。

方法一:使用unicode_escape 解碼
unicode = b'\\u4f60\\u597d'
re = unicode.decode("unicode_escape")
print(re)
返回:你好方法二:使用encode()方法轉(zhuǎn)換,再調(diào)用bytes.decode()轉(zhuǎn)換為字符串形式
s = r'\u4f60\u597d'
print(s.encode().decode("unicode_escape"))方法三: 使用json.loads 解碼(為json 格式)
str = '\u4eac\u4e1c\u653e\u517b\u7684\u722c\u866b'
print json.loads('"%s"' %str)方法四:使用eval(遇到Unicode是通過requests在網(wǎng)上爬取的時(shí)候)
response = requests.get(url,headers=headers)
re = eval("u"+"\'"+response.text+"\'")
print(re)以上就是小編整理的python中將unicode編碼轉(zhuǎn)換為中文的方法,希望能對(duì)你有所幫助喲~

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流