鱼C论坛

 找回密码
 立即注册
查看: 554|回复: 2

[已解决]第二十九讲,

[复制链接]
发表于 2020-8-20 11:41:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题1:标红的( (role,line_spoken) = each_line.split(':',1))怎么理解。
问题2:运行代码,没生成(boy_' + str(count)+'.txt之类文件)


boy =[]
girl =[]
count = 0
f=open('D:\\record2.txt')
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)
          girl_file.writelines(girl)
          boy=[]
          girl=[]
          count +=1

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()
boy_file.close()
f.close()



最佳答案
2020-8-20 11:45:37


你忘记关闭文件了在循环中,而且最后的关闭文件错了重复关闭 boy 的文件了

  1. boy =[]
  2. girl =[]
  3. count = 0
  4. f=open('D:\\record2.txt')
  5. for each_line in f:
  6.     if each_line[:6]!='======':
  7.         (role,line_spoken) = each_line.split(':',1)
  8.         if role =='小甲鱼':
  9.              boy.append(line_spoken)
  10.         if role =='小客服':
  11.              girl.append(line_spoken)
  12.     else:
  13.           file_name_boy = 'boy_' + str(count)+'.txt'
  14.           file_name_girl = 'girl_' + str(count)+'.txt'
  15.           boy_file = open(file_name_boy,'w')
  16.           girl_file = open(file_name_girl,'w')
  17.           boy_file.writelines(boy)
  18.           girl_file.writelines(girl)
  19.           boy_file.close()
  20.           girl_file.close()
  21.           boy=[]
  22.           girl=[]
  23.           count +=1

  24. file_name_boy = 'boy_' + str(count)+'.txt'
  25. file_name_girl = 'girl_' + str(count)+'.txt'
  26. boy_file = open(file_name_boy,'w')
  27. girl_file = open(file_name_girl,'w')
  28. boy_file.writelines(boy)
  29. girl_file.writelines(girl)
  30. boy_file.close()
  31. girl_file.close()
  32. f.close()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-20 11:45:37 | 显示全部楼层    本楼为最佳答案   


你忘记关闭文件了在循环中,而且最后的关闭文件错了重复关闭 boy 的文件了

  1. boy =[]
  2. girl =[]
  3. count = 0
  4. f=open('D:\\record2.txt')
  5. for each_line in f:
  6.     if each_line[:6]!='======':
  7.         (role,line_spoken) = each_line.split(':',1)
  8.         if role =='小甲鱼':
  9.              boy.append(line_spoken)
  10.         if role =='小客服':
  11.              girl.append(line_spoken)
  12.     else:
  13.           file_name_boy = 'boy_' + str(count)+'.txt'
  14.           file_name_girl = 'girl_' + str(count)+'.txt'
  15.           boy_file = open(file_name_boy,'w')
  16.           girl_file = open(file_name_girl,'w')
  17.           boy_file.writelines(boy)
  18.           girl_file.writelines(girl)
  19.           boy_file.close()
  20.           girl_file.close()
  21.           boy=[]
  22.           girl=[]
  23.           count +=1

  24. file_name_boy = 'boy_' + str(count)+'.txt'
  25. file_name_girl = 'girl_' + str(count)+'.txt'
  26. boy_file = open(file_name_boy,'w')
  27. girl_file = open(file_name_girl,'w')
  28. boy_file.writelines(boy)
  29. girl_file.writelines(girl)
  30. boy_file.close()
  31. girl_file.close()
  32. f.close()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-20 16:40:27 | 显示全部楼层
最后忘记关闭girl_file.close() 文件
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-22 10:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表