Python老男孩 day18 文件处理模式b模式

http://www.cnblogs.com/linhaifeng/articles/5984922.html

# f=open('test11.py','rb',encoding='utf-8') #报错,b的方式不能指定编码
f=open('test11.py','rb') 
data=f.read()
#'字符串'---------encode---------》bytes
#bytes---------decode---------》'字符串'
print(data)    
print(data.decode('utf-8'))
f.close()

运行结果:
b'hello1\r\n22222\r\n33333\r\n4444\r\n\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x95\x8a\xe6\x9e\x97\xe5\xb8\x88\xe5\x82\x85'
hello1
22222
33333
4444
你好啊林师傅


f=open('test22.py','wb') #b的方式不能指定编码
f.write(bytes('1111\n',encoding='utf-8'))
f.write('杨件'.encode('utf-8'))
#以上2种写入方式都可以

 

posted @ 2018-07-24 17:39  小飞侠Kobe  阅读(155)  评论(0编辑  收藏  举报