python將dict中的unicode打印成中文實(shí)例
我就廢話不多說了,大家還是直接看代碼吧!
import jsona = {u’content’: {u’address_detail’: {u’province’: u’u5409u6797u7701’, u’city’: u’u957fu6625u5e02’, u’street_number’: u’’, u’district’: u’’, u’street’: u’’, u’city_code’: 53}, u’point’: {u’y’: u’43.89833761’, u’x’: u’125.31364243’}, u’address’: u’u5409u6797u7701u957fu6625u5e02’}, u’status’: 0, u’address’: u’CN|u5409u6797|u957fu6625|None|CERNET|0|0’}print json.dumps(a).decode(’unicode-escape’)
輸出:
{'content': {'address_detail': {'province': '吉林省', 'city': '長春市', 'street_number': '', 'district': '', 'street': '', 'city_code': 53}, 'address': '吉林省長春市', 'point': {'y': '43.89833761', 'x': '125.31364243'}}, 'status': 0, 'address': 'CN|吉林|長春|None|CERNET|0|0'}
補(bǔ)充知識(shí):在python代碼中輸出一個(gè)字符的unicode編碼
如果ch是一個(gè)unicode字符:
print ’u%04x’ % ord(ch)
ord(ch)返回的是這個(gè)字符的unicode編碼的10進(jìn)制形式,只需要將其按照unicode的格式用16進(jìn)制打印出來即可
例如:
上面這個(gè)例子中就打印出了'你'、'好'、'a'這三個(gè)unicode字符的unicode碼。
unicode字符就是unicode字符串中的字符,對于字符串常量來說,以u為前綴的是unicode字符串;
如果一個(gè)是從utf-8文本文件中讀取的一行str,轉(zhuǎn)換過為unicode字符串只需要decode即可:
line = myfile.readline()uniline = line.decode(’utf-8’)
//此時(shí)uniline就是unicode字符串了,如果是gbk格式的文件,則要用gbk編碼來decode
以上這篇python將dict中的unicode打印成中文實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究2. 三個(gè)不常見的 HTML5 實(shí)用新特性簡介3. Angular獲取ngIf渲染的Dom元素示例4. IIS+PHP添加對webp格式圖像的支持配置方法5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. 無線標(biāo)記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁7. 使用.net core 自帶DI框架實(shí)現(xiàn)延遲加載功能8. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問題分析9. php測試程序運(yùn)行速度和頁面執(zhí)行速度的代碼10. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析
