|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 ztp 于 2019-9-11 22:02 编辑
如图,我把冒号中文英文都试了一次还是不能运行成功?是什么问题?
- count=1
- boy=[]
- girl=[]
- f=(r'C:\\Users\\Thor\\Desktop\\record.txt', encoding = 'UTF-8')
- 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_boy_name='boy_'+count+'.txt'
- file_girl_name='girl_'+count+'.txt'
- boy_file=open(file_boy_name,'w')
- girl_file=open(file_girl_name,'w')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- count=+1
- boy=[]
- girl=[]
-
- file_boy_name='boy_'+count+'.txt'
- file_girl_name='girl_'+count+'.txt'
- boy_file=open(file_boy_name,'w')
- girl_file=open(file_girl_name,'w')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- count=+1
- boy=[]
- girl=[]
- f.close()
复制代码
好3个地方是错误的。
1》count=+1,应该是count += 1
2》file_boy_name='boy_'+count+'.txt' ,count不是字符串,应该用str转换,写成file_boy_name='boy_'+str(count)+'.txt'
3》最后boy_file和girl_file,没有关闭文件
参考代码如下:
- count=1
- boy=[]
- girl=[]
- f=(r'C:\\Users\\Thor\\Desktop\\record.txt', encoding = 'UTF-8')
- 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_boy_name='boy_'+str(count)+'.txt'
- file_girl_name='girl_'+str(count)+'.txt'
- boy_file=open(file_boy_name,'w')
- girl_file=open(file_girl_name,'w')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- count+=1
- boy = []
- girl = []
-
- file_boy_name='boy_'+str(count)+'.txt'
- file_girl_name='girl_'+str(count)+'.txt'
- boy_file=open(file_boy_name,'w',encoding='utf-8')
- girl_file=open(file_girl_name,'w',encoding='utf-8')
- boy_file.writelines(boy)
- girl_file.writelines(girl)
- boy_file.close()
- girl_file.close()
- f.close()
复制代码
|
|