Python 中文编码

# -*- coding:utf-8 -*-
# ASCII 是一种单字节的编码,可表示256个不同字符
# 中文 在 python3 中默认用 unicode编码
lst = ['你', 	   # str类型,unicode编码
	str('你'), # 同上
	u'你', # 同上
	'你'.encode('utf-8').decode('utf-8'), # 同上
	# encode 将 str 转为 bytes 类型,可以再用 decode 转回 str 类型
	
	'你'.encode('utf-8'), # b'\xe4\xbd\xa0',utf-8编码,一个汉字 3 Byte
	'你'.encode('gbk'), # b'\xc4\xe3',gbk、gbxxxx 编码,一个汉字 2 Byte
	'你'.encode('GB2312') # 同上
	]

for word in lst:
	print (word, type(word))
posted @ 2018-09-10 00:03  水郁  阅读(317)  评论(0编辑  收藏  举报
……