马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# 将record文件里面的内容按照文中的“======”进行分割,并按照下面的要求使用pickle保存:
# 小甲鱼的对话单独保存为boy_*.txt的文件,并去掉“小甲鱼:”
# 小客服的对话单独保存为girl_*.txt的文件,并去掉“小客服:”
# 文中有两处“======”,那么对应会产生boy_1.txt、boy_2.txt、boy_3.txt和
# girl_1.txt、girl_2.txt、girl_3.txt这6个文件import pickle
def file_write(listname, role, count):
filename = role + str(count) + '.txt'
f = open(filename, 'wb')
for i in listname:
pickle.dump(i, f)
f.close()
def file_split(filename):
f = open(filename, 'r', encoding='utf-8')
count =1
boys = []
girls = []
for eachline in f:
if not('===' in eachline):
(role, speak) = eachline.split(':', 1)
if role == '小甲鱼':
boys.append(speak)
elif role == '小客服':
girls.append(speak)
else:
file_write(boys, 'boy', count)
file_write(girls, 'girl', count)
count += 1
boys = []
girls = []
file_write(boys, 'boy', count)
file_write(girls, 'girl', count)
f.close()
file_split('record.txt')
|