请帮忙注释下意思,谢谢
import pickledef save_file(boy, girl, count):
file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txt'
boy_file = open(file_name_boy, 'wb') # 记得一定要加 b 吖
girl_file = open(file_name_girl, 'wb') # 记得一定要加 b 吖
pickle.dump(boy, boy_file)
pickle.dump(girl, girl_file)
boy_file.close()
girl_file.close()
def split_file(file_name):
count = 1
boy = []
girl = []
f = open(file_name)
for each_line in f:
if each_line[:6] != '======':
(role, line_spoken) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
save_file(boy, girl, count)
boy = []
girl = []
count += 1
save_file(boy, girl, count)
f.close()
split_file('record.txt')
import pickle #导入pickle模块
def save_file(boy, girl, count): #创建一个save_file的函数括号中是三个需要传入的三个参数 (boy是个列表,girl是个列表,count是个数字)
file_name_boy = 'boy_' + str(count) + '.txt' #拼接需要存储的文件名
file_name_girl = 'girl_' + str(count) + '.txt' #同上
boy_file = open(file_name_boy, 'wb') # 记得一定要加 b 吖 #二进制模式创建文件
girl_file = open(file_name_girl, 'wb') # 记得一定要加 b 吖 #同上
pickle.dump(boy, boy_file)#把传入的boy的参数写入boy_file
pickle.dump(girl, girl_file)#把传入的girl的参数写入girl_file
boy_file.close() #关闭文件
girl_file.close()
def split_file(file_name):
count = 1
boy = []
girl = []
f = open(file_name)
for each_line in f:
if each_line[:6] != '======':
(role, line_spoken) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
save_file(boy, girl, count)
boy = []
girl = []
count += 1
save_file(boy, girl, count)
f.close()
split_file('record.txt')
写了一半 下班了哈哈哈 xuyuanyuanxyy 发表于 2020-9-18 18:30
import pickle #导入pickle模块
def save_file(boy, girl, count): #创建一个save_file的函数括 ...
大哥,有点专业精神好不好{:5_104:} #定义写入函数
def save_file(boy,girl,count): #传入两个列表(用于存储对话内容)和一个计数器
file_name_boy='boy_'+str(count)+'.txt'#新建一系列文件,命名由字符串拼接
file_name_girl='girl_'+str(count)+'.txt'#同理
boy_file=open(file_name_boy,'w')#以写入方式打开
girl_file=open(file_name_girl,'w') #同理
boy_file.writelines(boy) #写入
girl_file.writelines(girl)
boy_file.close() #关闭
girl_file.close()
#定义切分函数
def spilt_file(file_name): #传入文件名
f = open(file_name,"r",encoding='UTF-8')
#关键点,编码方式
boy=[]
girl=[]
count=1
for each_line in f: #遍历原对话文件每一行
if each_line[:6]!='======':
(role,line_spoken)=each_line.split(':',1)
#以:为界,分成元组,进行1次操作
if role =='小甲鱼': #根据名称(前三个字符)不同分别记入两个列表
boy.append(line_spoken)
if role =='小客服':
girl.append(line_spoken)
#到这一步,所有对话均被存入两列表
else:
save_file(boy,girl,count) #调用写入
boy=[]
girl=[]
count+=1
save_file(boy,girl,count)#第三段内容(上方for循环漏掉一次写入)
f.close()
spilt_file('record.txt')
import pickle
def save_file(boy, girl, count):
file_name_boy = 'boy_' + str(count) + '.txt' boy_xxx.txt
file_name_girl = 'girl_' + str(count) + '.txt' girl_xxx.txt
boy_file = open(file_name_boy, 'wb') # 记得一定要加 b 吖 "wb" 以二进制写方式打开,只能写文件, 如果文件不存在,创建该文件;如果文件已存在,先清空,再打开文件
girl_file = open(file_name_girl, 'wb') # 记得一定要加 b 吖 "wb" 以二进制写方式打开,只能写文件, 如果文件不存在,创建该文件;如果文件已存在,先清空,再打开文件
pickle.dump(boy, boy_file) Pickling”是将Python对象层次结构转换为字节流的过程, “unpickling”是反向操作,从而将字节流(来自二进制文件或类似字节的对象)转换回对象层次结构。pickle模块对于错误或恶意构造的数据是不安全的
pickle.dump(girl, girl_file)Pickling”是将Python对象层次结构转换为字节流的过程, “unpickling”是反向操作,从而将字节流(来自二进制文件或类似字节的对象)转换回对象层次结构。pickle模块对于错误或恶意构造的数据是不安全的
boy_file.close()
girl_file.close()
def split_file(file_name):
count = 1
boy = []
girl = []
f = open(file_name) 打开文件
for each_line in f: 信息筛选
if each_line[:6] != '======':
(role, line_spoken) = each_line.split(':', 1)
if role == '小甲鱼':
boy.append(line_spoken)
if role == '小客服':
girl.append(line_spoken)
else:
save_file(boy, girl, count)
boy = []
girl = []
count += 1
save_file(boy, girl, count) 调用save_file函数
f.close() 关闭文件
split_file('record.txt') 程序运行调用split_file函数
还剩一点点靠自己领悟咯 满意的话,记得给个最佳
页:
[1]