5.1读写文本数据
问题:如何读写各种编码的文本数据 方案:使用带有rt或者wt 模式的open()函数
with open('data_file/ch5_1.txt' ,'rt' ) as f:
'''读取整个文件为一个字符串'''
data = f.read()
print(data)
I love pyhthon
hello world
with open('data_file/ch5_1.txt' ,'rt' ) as f:
for line in f:
print(line,end=' ' )
I love pyhthon
hello world
text1 = 'My name is Jack'
text2 = "whay's your name?"
with open('data_file/ch5_2.txt' ,'wt' ) as f:
f.write(text1)
f.write(text2)
如果想要在已经存在的文件中继续添加内容,可以使用at模式,wt模式会覆盖掉原来的内容
with open('data_file/ch5_2.txt' ,'at' ) as f:
f.write('\n my name is FLC' )
文件的读写操作在不指定编码方式的时候使用的是系统编码,可以通过sys.getdefaultencoding()得到。也可以指定编码方式
with open('data_file/ch5_3.txt' ,'wt' ,encoding='latin-1' ) as f:
f.write('acb' )
5.2打印输出至文件
问题:将print()函数的输出重定向到一个文件中 方案:在print()函数中指定file关键字参数
with open('data_file/ch5_2.txt' ,'wt' ) as f:
print('HHHHH' ,file=f)
5.3使用其它分隔符或者终止符打印
问题:想使用print()函数输出数据,但是改变默认的分隔符或者行尾符 方案:可以在print()函数中使用sep和end参数
print('ACME' ,50 ,91.6 )
ACME 50 91.6
print('ACME' ,50 ,91.6 ,sep = ',' )
ACME,50,91.6
print('ACME' ,50 ,91.6 ,sep=',' ,end='!!\n' )
ACME,50,91.6!!
print(',' .join(('ACE' ,'50' ,'91.3' )))
ACE,50,91.3
print(',' .join(('ACE' ,50 ,91.3 )))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-23-0896b56400ea> in <module>()
----> 1 print(','.join(('ACE',50,91.3)))
TypeError: sequence item 1: expected str instance, int found
row = ('ACME' ,30 ,91.3 )
print(',' .join(str(r) for r in row))
ACME,30,91.3
print(*row,sep=',' )
ACME,30,91.3
5.4读写字节数数据
问题:如何读写二进制文件,如图片和声音文件 方案:使用rb模式或者wb模式进行读写
with open(filename,'rb' ) as f:
data = f.read()
pass
with open(filename,'wb' ) as f:
f.write(b'hello' )
pass
s = 'hello'
s[0 ]
'h'
t = b'hello'
t[0 ]
104
如果想要从二进制文件中读取或者写入文本数据,必须进行解码和编码操作
with open('data_file/ch5_4.bin' ,'wb' ) as f:
text = 'hello'
f.write(text.encode('utf-8' ))
with open('data_file/ch5_4.bin' ,'rb' ) as f:
data = f.read()
text = data.decode('utf-8' )
print(text)
hello
二进制I/O还有一个特性就是数组和C 结构体的直接能被写入,而不需要中间转换为自己的对象
import array
nums = array.array('i' ,[1 ,2 ,3 ,4 ])
with open('data_file/data.bin' ,'wb' ) as f:
f.write(nums)
with open('data_file/data.bin' ,'rb' ) as f:
print(f.read())
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00'
5.5文件不存在才能写入
问题:向一个文件中写入数据,前提是该文件在系统上不存在,否则原来的文件内容会被覆盖 方案:可以使用x模式来代替w方式
with open('data_file/somefile.txt' ,'wt' ) as f:
f.write('hello\n' )
with open('data_file/somefile.txt' ,'xt' ) as f:
f.write('world\n' )
---------------------------------------------------------------------------
FileExistsError Traceback (most recent call last)
<ipython-input-45-5256dbca7805> in <module>()
----> 1 with open('data_file/somefile.txt','xt') as f:
2 f.write('world\n')
FileExistsError: [Errno 17] File exists: 'data_file/somefile.txt'
import os
if not os.path.exists('somefile' ):
with open('data_file/somefile.txt' ,'wt' ) as f:
f.write('hello' )
else :
print('file already exist' )
5.6字符串的I/O操作
问题:想使用操作类文件对象的程序操作文本或者二进制字符串 方案:使用io.StringIO()和io.BytesIO()类来创建文件对象
import io
s = io.StringIO()
s.write('hello world\n' )
12
print('this is a test' ,file = s)
s.getvalue()
'hello world\nthis is a test\n'
s = io.StringIO('hello\nworld\n' )
s.read(4 )
'hell'
s.read()
'o\nworld\n'
如果要操作二进制数据,可以使用io.BytesIO()类操作
s = io.BytesIO()
s.write(b'binary data' )
s.getvalue()
b'binary data'
5.7读写压缩文件
问题:想读写一个gzip或者bz2格式的压缩文件 方案:使用gzip和bz2模块 说明:当写入压缩数据时可以使用compresslevel这个可选的参数,来制定压缩等级。默认的是最高等级9,等级越低性能越好,但是数据的压缩程度也低
import gzip
text = 'hhhhhhhhhhhhadfafaf'
with gzip.open('data_file/data2.gz' ,'wt' ,compresslevel=5 ) as f:
f.write(text)
import gzip
with gzip.open('data_file/data2.gz' ,'rt' ) as f:
text = f.read()
print(text)
hhhhhhhhhhhhadfafaf
import bz2
with bz2.open(filename,'rt' ) as f:
text = f.read()
pass
with bz2.open(filename,'wt' ) as f:
f.write(text)
pass
此外,gzip.open()和bz2.open()可以作用在一个已经存在并且以二进制模式打开的文件上,这样可以允许gzip、bz2模块工作在许多类文件上,如套接字、管道、和内存中文件
import gzip
f = open('data_file/data2.gz' ,'rb' )
with gzip.open(f,'rt' ) as g:
text = g.read()
print(text)
hhhhhhhhhhhhadfafaf