在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ 在python中print "unicode中文所組成list&quo

在python中print "unicode中文所組成list"的list, 僅顯示unicode編碼

問題描述

創(chuàng)建了一個list:
test=[[u'測試',u'學(xué)習(xí)'],[u'提問',u'回答']]
然后用print打印的時候顯示的是
[[u'u6d4bu8bd5', u'u5b66u4e60'], [u'u63d0u95ee', u'u56deu7b54']]

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

在網(wǎng)上找了一些方法比如“ ”.join(XXX)、encode...decode之類的,都沒有成功

相關(guān)代碼

// 請把代碼文本粘貼到下方(請勿用圖片代替代碼)
test=[[u'測試',u'學(xué)習(xí)'],[u'提問',u'回答']]
print test

你期待的結(jié)果是什么?實(shí)際看到的錯誤信息又是什么?

期待看到[['測試','學(xué)習(xí)'],['提問','回答']]
然而實(shí)際上輸出的是[[u'u6d4bu8bd5', u'u5b66u4e60'], [u'u63d0u95ee', u'u56deu7b54']]

回答
編輯回答
青檸

python2 的編碼問題確實(shí)很令人頭疼.
對于你遇到的情況, 首先應(yīng)該把 unicode 類型轉(zhuǎn)換成 utf-8 格式進(jìn)行展示.

# coding: utf-8

test=[[u'測試',u'學(xué)習(xí)'],[u'提問',u'回答']]
# 換成 utf-8 的字節(jié)碼
tester = [[y.encode('utf-8') for y in x] for x in test]

# print 展示
print(repr(tester).decode('string-escape'))

使用 python3 吧, 這些類似的編碼問題基本不會出現(xiàn)的.

2017年7月14日 15:19
編輯回答
生性
import json
test=[[u'測試',u'學(xué)習(xí)'],[u'提問',u'回答']]
print json.dumps(test,ensure_ascii=False)

不好用你打我.

2017年9月1日 23:26
編輯回答
吢丕
>>> test=[[u'測試',u'學(xué)習(xí)'],[u'提問',u'回答']]           
>>> print(test)
[['測試', '學(xué)習(xí)'], ['提問', '回答']]
>>> 

Python3 根本不用任何操作。哈哈

2017年10月19日 04:41