本帖最后由 jackz007 于 2022-9-14 23:04 编辑
f = open(r"D:\record.txt.txt" , encoding = "UTF-8")
boy , girl , count = [] , [] , 1
for each_line in f:
if each_line[: 6] != "======" : # 【修改】:01
try:
(role , line_spoken) = each_line . split(":" , 1)
if role == "小甲鱼":
boy . append(line_spoken)
if role == "小客服":
girl . append(line_spoken)
except:
print('* Error * :' , each_line)
else:
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) # 【修改】:02
girl_file . writelines(girl) # 【修改】:03
boy_file . close()
girl_file . close()
boy , girl , = [] , []
count += 1
f . close()
if boy:
file_name_boy = "boy_" + str(count) + ".txt"
boy_file = open(file_name_boy , "w")
boy_file . writelines(boy)
boy_file . close()
if girl:
file_name_girl = "girl" + str(count) + ".txt"
girl_file = open(file_name_girl , "w")
girl_file . writelines(girl)
girl_file . close()
运行这个代码,如果没有屏显信息,那就是没有错误,否则,无法处理的那行文件内容会被显示出来。 |