|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#以下是自己独立实现的版本
f = open("record.txt",'r')
fboy_1 = open("boy_1.txt",'w')
fgirl_1 = open("girl_1.txt",'w')
fboy_2 = open("boy_2.txt",'w')
fgirl_2 = open("girl_2.txt",'w')
fboy_3 = open("boy_3.txt",'w')
fgirl_3 = open("girl_3.txt",'w')
number = 1
for echo in f:
if echo[:3] == "小甲鱼":
if number == 1:
fboy_1.write(echo[4:])
elif number == 2:
fboy_2.write(echo[4:])
elif number == 3:
fboy_3.write(echo[4:])
elif echo[:3] == '小客服':
if number == 1:
fgirl_1.write(echo[4:])
elif number == 2:
fgirl_2.write(echo[4:])
elif number == 3:
fgirl_3.write(echo[4:])
elif echo[:3] == '===':
if number == 1:
fboy_1.close()
fgirl_1.close()
number = 2
elif number == 2:
fboy_2.close()
fgirl_2.close()
number = 3
elif number == 3:
fboy_3.close()
fgirl_3.close()
f.close()
#以下是看完小甲鱼实现以后自己敲的代码
f = open("record.txt",'r')
name_list = ['小甲鱼','小客服']
boy_list = []
girl_list = []
number = 1
for echo in f:
if echo[:3] != '===':
(name,content) = echo.split(":",1)
if name == name_list[0]:
boy_list.append(content)
elif name == name_list[1]:
girl_list.append(content)
else:
boy_file_name = "boy_" + str(number) + '.txt'
girl_file_name = 'girl_' + str(number) + '.txt'
boy_file = open(boy_file_name,'w')
girl_file = open(girl_file_name,'w')
boy_file.writelines(boy_list)
girl_file.writelines(girl_list)
boy_file.close()
girl_file.close()
boy_list.clear()
girl_list.clear()
number += 1
boy_file_name = "boy_" + str(number) + '.txt'
girl_file_name = 'girl_' + str(number) + '.txt'
boy_file = open(boy_file_name,'w')
girl_file = open(girl_file_name,'w')
boy_file.writelines(boy_list)
girl_file.writelines(girl_list)
boy_file.close()
girl_file.close()
f.close()
#小甲鱼实现的更好,程序结构框架也很好,把以前字符串列表等知识都用起来了,融会贯通。不得不说小甲鱼还是小甲鱼,你大爷还是你大爷。路漫漫,还需要继续学习。 |
|