python复习1

1:#r模式,在文件不存在时,不会创建新的文件

  f = open('a.txt','r',encoding = 'utf-8')

2:字符编码

  什么是字符编码?

    把人类的字符翻译成计算机能认识的数字

  什么是字符编码表?

    ASCII

    GBK

    UTF-8

    Unicode

unicode-------->encode('utf-8')-------------->bytes

bytes------------>decode('utf-8')--------------->unicode

原则:

  字符以什么格式编译的,就要以什么格式解码

ps:

  python3中的字符分为两种

  x='egon'存成unicode

  y=x.encode('utf-8')存成bytes

 

python2中的字符串也分成两种

   x = u'egon' 与python3中的字符串相同

  y='alex'与python3中的bytes类型相同

 

#r模式的读,在文件不存在时,不会创造新的文件
f = open('a.txt','r',encoding = 'utf-8')
print(f.read())
f.close()


#b模式即直接从硬盘中读取bytes
print(f.read().decode('utf-8'))

#w文本模式的写,文件存在则清空,不存在则创建
f = open('a.txt','w',encoding = 'utf-8')
f = open('b.txt','w',encoding = 'utf-8')
print(f.writeable())#判断是否可写
print(f.readable())#判断是否可读

#a文本模式的追加
f = open('b.txt','a',encoding = 'utf-8')
print(f.writeable())#写文件涉及到文件光标的移动
print(f.tell())
print(f.readline())
f.write('333\n')
#r+,w+,a+,代表读的时候可以写,写的时候可以读

#rb模式即直接从硬盘中读取bytes
f = open('a.txt','rb')
print(f.read().decode('utf-8'))

#wb模式
f = open('a.txt','wb')
f.write('你好啊'.encode('utf-8'))


##b模式可以读取任何模式的文件
with open('test.jpg','rb') as read_f,\
open('test1.jpg','wb') as write_f:
for line in read_f:
write_f.write(line)

转载于:https://www.cnblogs.com/huangxiaohan/p/7778444.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值