1. [record.txt](https://www.yuque.com/attachments/yuque/0/2022/txt/10367081/1644767218997-f31cac8c-ccb3-4d67-8495-28f10ae20bc2.txt?_lake_card=%7B%22src%22%3A%22https%3A%2F%2Fwww.yuque.com%2Fattachments%2Fyuque%2F0%2F2022%2Ftxt%2F10367081%2F1644767218997-f31cac8c-ccb3-4d67-8495-28f10ae20bc2.txt%22%2C%22name%22%3A%22record.txt%22%2C%22size%22%3A967%2C%22type%22%3A%22text%2Fplain%22%2C%22ext%22%3A%22txt%22%2C%22source%22%3A%22%22%2C%22status%22%3A%22done%22%2C%22mode%22%3A%22title%22%2C%22download%22%3Atrue%2C%22taskId%22%3A%22ud994d872-523a-4883-bd07-c47841eeb9e%22%2C%22taskType%22%3A%22upload%22%2C%22id%22%3A%22u5b7d886b%22%2C%22card%22%3A%22file%22%7D)<br />![image.png](https://cdn.nlark.com/yuque/0/2022/png/10367081/1644761839460-dde56ee2-9357-4dcb-9c81-fe238c6fe29c.png#clientId=u027d8d3c-4c8c-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=349&id=u68da8376&margin=%5Bobject%20Object%5D&name=image.png&originHeight=528&originWidth=778&originalType=binary&ratio=1&rotation=0&showTitle=false&size=57715&status=done&style=none&taskId=u20f37a70-a7ac-4e6e-bcef-04fcac12200&title=&width=514)<br />任务:将文件(record.txt)中的数据进行分割并按照以下规律保存起来:
  1. 张三的对话单独保存为boy_*.txt的文件(去掉张三:”)
  2. 客服的对话单独保存为girl_*.txt的文件(去掉客服:”)
  3. 文件中总共有三段对话,分别保存为
    1. boy_1.txt
    2. girl_1.txt
    3. boy_2.txt
    4. girl_2.txt
    5. boy_3.txt
    6. gril_3.txt

共6个文件(提示:文件中不同的对话间已经使用 =======分割)

方式1

  1. # 1.首先打开目标文件
  2. f = open('record.txt')
  3. print('结果:',f)
  4. boy = []
  5. girl = []
  6. count = 1
  7. # 2.获取一行的数据
  8. # 3. 判断当前行是否有等号
  9. for each_line in f:
  10. if each_line[:6] != "======":
  11. (role, line_spoken) = each_line.split(':', 1)
  12. if role == '张三':
  13. boy.append(line_spoken)
  14. if role == '客服':
  15. girl.append(line_spoken)
  16. else:
  17. file_name_boy = 'boy_' + str(count) + '.txt'
  18. file_name_girl = 'girl_' + str(count) + '.txt'
  19. boy_file = open(file_name_boy, 'w')
  20. girl_file = open(file_name_girl, 'w')
  21. bb = boy_file.writelines(boy)
  22. print('bb:', bb)
  23. girl_file.writelines(girl)
  24. boy_file.close()
  25. girl_file.close()
  26. boy = []
  27. girl = []
  28. count += 1
  29. file_name_boy = 'boy_' + str(count) + '.txt'
  30. file_name_girl = 'girl_' + str(count) + '.txt'
  31. boy_file = open(file_name_boy, 'w')
  32. girl_file = open(file_name_girl, 'w')
  33. bb = boy_file.writelines(boy)
  34. print('bb:', bb)
  35. girl_file.writelines(girl)
  36. boy_file.close()
  37. girl_file.close()
  38. f.close()

方式2 (封装改良)

  1. def save_file(boy, girl, count):
  2. # 设置保存的文件名
  3. file_name_boy = 'boy_' + str(count) + '.txt'
  4. file_name_girl = 'girl_' + str(count) + '.txt'
  5. # 打开文件
  6. boy_file = open(file_name_boy, 'w')
  7. girl_file = open(file_name_girl, 'w')
  8. # 写入内容
  9. boy_file.writelines(boy)
  10. girl_file.writelines(girl)
  11. # 关闭文件
  12. boy_file.close()
  13. girl_file.close()
  14. def split_file(file_name):
  15. f = open(file_name)
  16. boy = []
  17. girl = []
  18. count = 1
  19. for each_line in f:
  20. if each_line[:6] != '======':
  21. (role, line_spoken) = each_line.split(':', 1)
  22. if role == '张三':
  23. boy.append(line_spoken)
  24. if role == '客服':
  25. girl.append(line_spoken)
  26. else:
  27. save_file(boy, girl, count)
  28. boy = []
  29. girl = []
  30. count += 1
  31. save_file(boy, girl, count)
  32. f.close()
  33. split_file('record.txt')