鱼C论坛

 找回密码
 立即注册
查看: 1132|回复: 1

[已解决]第八章 永久存储 文件

[复制链接]
发表于 2021-7-30 17:16:02 | 显示全部楼层 |阅读模式

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

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

x
代码在运行的时候出现了一些问题,帮忙看一下,谢谢
count = 1
boy = []
girl = []

f = open(r"C:\Users\lijin\Desktop\record.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()
girl_file.close()
f.close()



Traceback (most recent call last):
  File "C:\Users\lijin\Desktop\课堂练习\test.py", line 9, in <module>
    (role, line_spoken) = each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)

   
            

最佳答案
2021-7-30 17:29:23
if each_line[:6] != "========":
这里不应该是8个等号,[:6]一共有六个字符~~
  1. count = 1
  2. boy = []
  3. girl = []

  4. f = open(r"C:\Users\lijin\Desktop\record.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 = []
  20.     girl = []
  21.     count += 1

  22. file_name_boy = 'boy_' + str(count) + '.txt'
  23. file_name_girl = 'girl_' + str(count) + '.txt'

  24. boy_file = open(file_name_boy,'w')
  25. girl_file = open(file_name_girl,'w')

  26. boy_file.writelines(boy)
  27. girl_file.writelines(girl)

  28. boy_file.close()
  29. girl_file.close()
  30. f.close()
复制代码
屏幕截图 2021-07-30 171241.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-30 17:29:23 | 显示全部楼层    本楼为最佳答案   
if each_line[:6] != "========":
这里不应该是8个等号,[:6]一共有六个字符~~
  1. count = 1
  2. boy = []
  3. girl = []

  4. f = open(r"C:\Users\lijin\Desktop\record.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 = []
  20.     girl = []
  21.     count += 1

  22. file_name_boy = 'boy_' + str(count) + '.txt'
  23. file_name_girl = 'girl_' + str(count) + '.txt'

  24. boy_file = open(file_name_boy,'w')
  25. girl_file = open(file_name_girl,'w')

  26. boy_file.writelines(boy)
  27. girl_file.writelines(girl)

  28. boy_file.close()
  29. girl_file.close()
  30. f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-4 20:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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