python2 unicode bytes_了解python中bytes,str和unicode的区别

首先来说把Unicode转换为为原始8位值(二进制数据),有很多种办

编写Python程序的时候,核心部分应该用Unicode来写,也就是python3中的str,python2中的unicode

python3中2种表示字符序列的类型:bytes和str

前者的实例包含了原始8位值,后者的实例包含了Unicode字符

python3中接受bytes和str,并总是返回str:

def to_str(bytes_or_str):

if isinstance(bytes_or_str, bytes):

return bytes_or_str.decode("utf-8")

return bytes_or_str

python3中接受bytes和str,并总是返回bytes:

def to_bytes(bytes_or_str):

if isinstance(bytes_or_str, str):

return bytes_or_str.encode("utf-8")

return bytes_or_str

python2中2种表示字符序列的类型:unicode和str

与python3刚好相反:后者的实例包含了原始8位值,前者的实例包含了Unicode字符

python2中接受unicode和str,并总是返回unicode:

def to_str(bytes_or_str):

if isinstance(bytes_or_str, str):

return bytes_or_str.decode("utf-8")

return bytes_or_str

python2中接受unicode和str,并总是返回str:

def to_bytes(bytes_or_str):

if isinstance(bytes_or_str, unicode):

return bytes_or_str.encode("utf-8")

return bytes_or_str

python2和python3需要注意的事情

1.python2中如果str只包含7位的ASCII字符,那么unicode和str 就是同一种类型,可以+操作

2.python3内置的open函数获取文件句柄,默认采用utf-8的格式操作文件,python2则默认是二进制

python2 的写法:

with open("/temp/file.bin","w")as f :

f.write(os.urandom(10))

python3 的写法:

with open("/temp/file.bin","wb")as f :

f.write(os.urandom(10))

ps:如何让你的代码pythonic

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值