|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
f =open(r"D:\record.txt.txt",encoding="UTF-8")
boy =[]
girl =[]
count =1
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:
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_file)
girl_file.writelines(girl_file)
boy_file.close()
girl_file.close()
boy =[]
girl =[]
count +=1
f.close() ValueError: not enough values to unpack (expected 2, got 1)(没有足够的值来解包(应为2,得到1)) #########有没大佬请教一下,谢谢 (role,line_spoken) =each_line.split(":",1) 估计是这一行错了吗
本帖最后由 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()
复制代码
运行这个代码,如果没有屏显信息,那就是没有错误,否则,无法处理的那行文件内容会被显示出来。
|
|