python-day08_文件_编码二
1,文件操作。
模特主妇护士老师.txt
1,文件路径:d:\模特主妇护士老师.txt
2,编码方式:utf-8 gbk 。。。。
3,操作方式:只读,只写,追加,读写,写读.....
以什么编码方式储存的文件,就以什么编码打开进行操作。
只读:r
rb #字节类型读,用于非文字文件,如图像文件等
f = open('模特主妇护士班主任',mode='r',encoding='utf-8')
content = f.read() #字符串类型
print(content,type(content))
f.close()
# f = open('模特主妇护士班主任',mode='rb',)
# content = f.read()
# print(content)
# f.close()
r+ 读写
r+b 读写(以bytes类型)
# f = open('log',mode='r+',encoding='utf-8')
# print(f.read())
# f.write('大猛,小孟')
# f.close()
#前面读过,光标就在最后,写的就加在最后
#如先写操作,新增有多少内容,会覆盖原文件前面的多少内容
只写:w
wb
# 先将源文件的内容全部清除,再写。
# f = open('log',mode='w',encoding='utf-8')
# f.write('附近看到类似纠纷')
# f.close()
f = open('log',mode='wb')
f.write('附近看到类似纠纷'.encode('utf-8'))
f.close()
w+
# f = open('log',mode='w+',encoding='utf-8')
# f.write('aaa')
# f.seek(0) #调光标到文件最前面,才能完成全文件读
# print(f.read())
# f.close()
w+b
.......
追加: a,从文件尾追加
a+
# f = open('log',mode='a',encoding='utf-8')
# f.write('佳琪')
# f.close()
# f = open('log',mode='a+',encoding='utf-8')
# f.write('佳琪')
# f.seek(0)
# print(f.read())
# f.close()
# f = open('log',mode='ab')
# f.write('佳琪'.encode('utf-8'))
# f.close()
# obj = open('log',mode='r+',encoding='utf-8')
# content = f.read(3) # 从光标开始,读出来3个字符(一个中文或一个字母都是一个字符)
# f.seek(3) # 按照字节单位放置光标的位置
# f.tell() 告诉你光标的位置,以字节为单位
# print(f.tell())
# content = f.read(2) #指定从光标所在位置开始,读2个字符。光标位置不对,汉字乱码程序会报错
# print(content)
# f.readable() # 是否可读,返加bool型
# line = f.readline() # 一行一行的读,str
# line = f.readlines() # 一次读完整个文件,每一行当成列表中的一个元素,添加到list中
# f.truncate(4) #从光标位置开始算,第4个字节以后的内容截掉。汉字打散可能会乱码
# for line in f:
# print(line)
# f.close()
程序结束会自动关闭打开的文件
# with open('log',mode='r+',encoding='utf-8') as f,\
# open('log2',mode='w+',encoding='utf-8') as f1:
# print(f)
line = f.readline()
while line: #读到文件尾时结束循环
print(line)
line = f.readline()
2,编码二
字符串在Python内部的表示是Unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,
即先将其他编码的字符解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
#str --->byte encode 编码
# s = '二哥'
# b = s.encode('utf-8')
# print(b)
# #byte --->str decode 解码
# s1 = b.decode('utf-8')
# print(s1)
# s = 'abf'
# b = s.encode('utf-8')
# print(b)
# #byte --->str decode 解码
# s1 = b.decode('gbk')
# print(s1)
浙公网安备 33010602011771号