在小甲鱼视频的新手入门Python的29课出现错误提示~ 哪位大佬解释一下
程序代码:file1 = open('f:/record.txt')
girl = []
boy = []
count = 1
for each_line in file1:
if each_line[:6] != '======':
(role,spoken_line) = each_line.split(':',1)
if role == '小甲鱼':
boy.append(spoken_line)
if role == '小客服':
girl.append(spoken_line)
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)
boy_file.close()
girl_file.close()
boy = []
girl = []
count += 1
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)
boy_file.close()
girl_file.close()
file1.close()
错误提示:
Traceback (most recent call last):
File "E:/Python文档/文件的保存打开更改~copy.py", line 7, in <module>
(role,spoken_line) = each_line.split(':',1)
ValueError: not enough values to unpack (expected 2, got 1)
我是按照视频上的程序一步步来的
你的软件的冒号和文档中的是不是一样,
如果不是这问题
把txt所有空行删掉,
不删的话,用这个代码
file1 = open('a.txt')
girl = []
boy = []
count = 1
for each_line in file1:
if each_line.strip():
if each_line[:6] != '======':
print(each_line,'xxx')
(role,spoken_line) = each_line.split(':',1)
if role == '小甲鱼':
boy.append(spoken_line)
if role == '小客服':
girl.append(spoken_line)
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)
boy_file.close()
girl_file.close()
boy = []
girl = []
count += 1
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)
boy_file.close()
girl_file.close()
file1.close() 塔利班 发表于 2018-10-21 22:12
你的软件的冒号和文档中的是不是一样,
如果不是这问题
把txt所有空行删掉,
代码也看过了 复制了你的代码可以正常运行,和我的代码对照了一下 没有什么差别啊 那有没有空行吧
if each_line.strip():这里过滤了空行
你的没有 塔利班 发表于 2018-10-22 16:36
那有没有空行吧
if each_line.strip():这里过滤了空行
你的没有
过滤空行是什么意思?
页:
[1]