马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 莫急莫急 于 2022-2-19 12:55 编辑
如图所示,改来改去还是改不好,然后干脆照抄小甲鱼的代码,第五行还是报错噢~
请各位兄弟姐妹帮我看一看吧,谢谢了~
f = open('C:/Users/22605/Desktop/record.txt')
boy=[]
girl=[]
count=1
for each_line in f:
if each_line[:6]!='======':
(role,line_spoken)=each_line.split(':',1)
if role=='1':
boy.append(line_spoken)
if role=='0':
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)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
boy=[]
girl=[]
count += 1
file_name_boy='boy'+str(count)+'text'
file_name_girl='girl'+str(count)+'text'
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()
f.close()
我猜你大概率是编码问题,在打开record.txt的时候加个编码就行,
f = open('C:/Users/22605/Desktop/record.txt', encoding='utf-8') # 这里加个encoding,如果 utf-8 还出错就把 utf-8 改成 gbk,因为通常都是这两个编码,我也不知道你是哪个
boy=[]
girl=[]
count=1
for each_line in f:
if each_line[:6]!='======':
(role,line_spoken)=each_line.split(':',1)
if role=='1':
boy.append(line_spoken)
if role=='0':
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)
girl_file.writelines(girl)
boy_file.close()
girl_file.close()
boy=[]
girl=[]
count += 1
file_name_boy='boy'+str(count)+'text'
file_name_girl='girl'+str(count)+'text'
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()
f.close()
|