马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在《零基础Python课后练习》中的31课,用二进制保存了文件内容在txt档中,
但是我希望读取 txt档中的二进制文件,发现使用open指令是出错的,应该怎么办呢?
程序如下,附件有原文件
import pickle
def save_file(boy, girl, count):
boy_file_name = 'boy_' + str(count) + '.txt'
girl_file_name = 'girl_' + str(count) + '.txt'
boy_file = open(boy_file_name, 'wb')
girl_file = open(girl_file_name, 'wb')
pickle.dump(boy, boy_file)
pickle.dump(girl, girl_file)
boy_file.close()
girl_file.close()
def operation_file(file_name):
count = 1
boy = []
girl = []
file = open(file_name)
for each_line in file:
if each_line[: 6] != '======':
(role, spoken) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(spoken)
if role == '小客服':
girl.append(spoken)
else:
save_file(boy, girl, count)
boy = []
girl = []
count += 1
save_file(boy, girl, count)
file.close()
operation_file('record.txt')
用上述程序,使用二进制保存内容到了txt档内,但是应该怎么样再次读取哪些内容呢?
求助。。
file = open(r'D:\Python Play\boy_1.txt', 'rb')
|